0

I have been trying to migrate Swift project to latest Swift version.

fileprivate lazy var _uploadedSurveysController: NSFetchedResultsController? = nil 

This code line gives the following error.

Nil cannot initialize the specified type NSFetchedResultsController? 

Xcode comes up with a fix recommendation which is adding another question mark next to NSFetchedResultsController. When i say yes, it gives the same error and coming up with a fix adding another question mark.

Here is more code

fileprivate var uploadedSurveysController: NSFetchedResultsController { guard _uploadedSurveysController == nil else { return _uploadedSurveysController! } _uploadedSurveysController = NSFetchedResultsController(fetchRequest: coreDataHelper.mailedAssetsFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "section2Identifier", cacheName: nil) do { try _uploadedSurveysController!.performFetch() } catch let error as NSError { Logger.sharedInstance.logMessage("\(#function) Uploaded Surveys Fetching Error: \(error.userInfo)") } return _uploadedSurveysController! } 

Any help would be really appreciated.

Just to let you know. I am really new to Swift and trying to update an existing project from Swift 2.3 to 3.

Thanks in advance. Remzi.

3
  • NSFetchedResultsController in Swift 3 needs a generic type declaration now. Can you include some more code where _uploadedSurveysController is used? (Initialised as not being nil) Commented Nov 3, 2016 at 11:20
  • Just try by removing '= nil' as your variable is already optional and can be nil or something so whenever you are going to use, you will check for nil before using this controller. Commented Nov 3, 2016 at 11:23
  • Hi Anni, i took '= nil' out but didnt do anything, different errors came up. Commented Nov 3, 2016 at 11:52

3 Answers 3

1

I came across this same error message after upgrading a Swift 2 project to Swift 3. Lazy instantiation has become simpler in Swift so there is no need for the hidden _variableName property any longer.

If you add the lazy attribute your property definition can you can then specify a function block to run to initialise it should the property ever be set to nil.

fileprivate lazy var uploadedSurveysController: NSFetchedResultsController<NSFetchRequestResult> = { var fetchRequest<NSFetchRequestResult> = NSFetchedResultsController(fetchRequest: coreDataHelper.mailedAssetsFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "section2Identifier", cacheName: nil) do { try fetchedResultsController.performFetch() } catch let error as NSError { Logger.sharedInstance.logMessage("\(#function) Uploaded Surveys Fetching Error: \(error.userInfo)") } return fetchedResultsContoller }() 

I found a good description on lazy instantiation in Swift in a blog entry by Mike Buss

Sign up to request clarification or add additional context in comments.

Comments

0

Here is more code.

fileprivate var uploadedSurveysController: NSFetchedResultsController { guard _uploadedSurveysController == nil else { return _uploadedSurveysController! } _uploadedSurveysController = NSFetchedResultsController(fetchRequest: coreDataHelper.mailedAssetsFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "section2Identifier", cacheName: nil) do { try _uploadedSurveysController!.performFetch() } catch let error as NSError { Logger.sharedInstance.logMessage("\(#function) Uploaded Surveys Fetching Error: \(error.userInfo)") } return _uploadedSurveysController! 

}

1 Comment

You should edit the original question to add this information.
0

I think it's because of the lazy designation, which follows different rules than a standard property. Since you've already implemented lazy instantiation on your own, the lazy keyword is unnecessary (and it's not intended for use in this manner anyway). Try removing it and see if it works.

2 Comments

Hi Bob, i have tried it but still seems the same error. Xcode comes up adding another question mark to fix it.
By the way i just removed lazy but left '= nil'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.