Just watched Whats new in Swift
in WWDC videos, they have made a number of changes to SDK and the APIs in it. So when heard about the NSManagedObject
fetchRequest
thing, I directly jumped in and started implementing it.
So as per the WWDC, the old NSManagedObject fetching model changed completely. This is a snippet I wrote in Swift 2.2, long back.
let answers : Answers = NSEntityDescription.insertNewObjectForEntityForName(EntityNames.Answers.rawValue, inManagedObjectContext: managedContext) as! Answers
Now I tried to do the same in Swift 3.0, here we go.
let answers : NSFetchRequest<Answers> = Answers.fetchRequest // WWDC video style
Then I got this error
Cannot convert value of type '() -> NSFetchRequest< NSFetchRequestResult >' to specified type 'NSFetchRequest<Answers>'
Then I changed the code to this
let answers : NSFetchRequest<Answers> = Answers.fetchRequest()
Then I got this error
Cannot convert value of type 'NSFetchRequest<Answers>' to specified type 'NSFetchRequest<NSFetchRequestResult>'
So I have changed the code a bit and the error gone away. But now I don't have an instance of Answers
NSManagedObject
, I have an instance of something else, by using this instance I can't access the properties of my Answers
NSManagedObject
. I want to do fetchRequest
in Answers.
What do you think about this? Let me know your comments please.
So I have solved it by using this way
extension Answers {
@nonobjc class func fetchRequest() -> NSFetchRequest<Answers> {
return NSFetchRequest<Answers>(entityName: "Level");
}
}
And fetched like this
let answers : NSFetchRequest<Answers> = Answers.fetchRequest()
Then I thought of making it more precise, like this
let answers = Answers.fetchRequest()
And shown the error Ambiguous use of fetchRequest()
Why??