Search code examples
iosxcodeswiftwritetofile

Value of type 'StartMorgagesViewPage2' has no member 'pdfData2'


I created a variable called pdfData :

 var pdfData: NSData {
    let result = NSMutableData()
    UIGraphicsBeginPDFContextToData(result, frame, nil)
    guard let context = UIGraphicsGetCurrentContext() else { return result }
    UIGraphicsBeginPDFPage()
    layer.renderInContext(context)
    UIGraphicsEndPDFContext()
    return result
 }

but then when I reference it:

   self.pdfData.writeToURL(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent("Page2.pdf"), atomically: true) // what it is saved as


    self.pdfData.writeToFile("Page2.pdf", atomically: false)

I get two errors 'Value of type 'StartMorgagesViewPage2' has no member 'pdfData2'. Here's my code:

import UIKit


class StartMorgagesViewPage2: UIView {

    override func didMoveToSuperview() {

        var gameTimer = NSTimer!()
        gameTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "screenshot", userInfo: nil, repeats: false)


        /*  label.text = "\(instructedbyvalue)"
         accountnumberlabel.text = "\(accountnumbersvalue)" //(boo) //"boooooo"
         applicantslabel.text = "\(applicantsvalue)"
         propertyaddresslabel.text = "\(propertyaddressvalue)"
         eircodedetails.text = "\(eircodedetailsvalue)"
         */


        //Saving


    }


    func screenshot() {
        print("screenshot")

        var pdfData: NSData {
            let result = NSMutableData()
            UIGraphicsBeginPDFContextToData(result, frame, nil)
            guard let context = UIGraphicsGetCurrentContext() else { return result }
            UIGraphicsBeginPDFPage()
            layer.renderInContext(context)
            UIGraphicsEndPDFContext()
            return result
        }




        self.pdfData.writeToURL(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent("Page2.pdf"), atomically: true) // what it is saved as


        self.pdfData.writeToFile("Page2.pdf", atomically: false)
        print(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!.path!)




    }




}

Here's a picture of the errors: my errors .

I can't think of a reason this could happen. I've tried Cleaning, Building and running the app.


Solution

  • The problem is you're trying to access your pdfData through using self – but pdfData isn't an instance member of self, it's just a plain old variable (albeit a calculated one).

    The solution therefore is to simply remove the self:

    pdfData.writeToFile("Page2.pdf", atomically: false)
    

    You should also note that you should prefer to use self implicitly when referring to instance members (properties or methods) while in that class, as it makes your code more readable. For example – using someMethodCall() instead of self.someMethodCall().