Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am migrating to Swift 3 and have come across a very strange error message while migrating abstract CoreData query code. entityName is passed to the following method:

func objects(entityName name:String)->[NSManagedObject]? {   
  let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName:name)
  var objects: [NSManagedObject]?
  do {
    objects = try managedObjectContext.fetch(fetchRequest)
  } catch { ... }
}

This results in the following error:

Cannot invoke 'fetch' with an argument list of type '(NSFetchRequest<NSFetchRequestResult>)' Expected an argument list of type '(NSFetchRequest<NSFetchRequestResult>)'

The error is stating I can't use the type its expecting. Is it possible to make abstract calls to CoreData like this in Swift 3?

The post How to apply the type to a NSFetchRequest instance? is what lead me this far.

I tried to cast fetchRequest, but it didn't change anything. managedObjectContext.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

do {
    objects = try managedObjectContext.fetch(fetchRequest) as! [YourEntityName]
  } catch {
  print(error)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...