小编典典

如何在iOS 10 Swift 3中初始化NSFetchedResultsController

swift

我无法NSFetchedResultsController使用AECoreDataUI的CoreDataTableViewController中的
Swift 3在iOS 10中进行初始化。

let request = NSFetchRequest<NasaPicture>(entityName:"NasaPicture")  
request.predicate = Predicate(format: "isPagedResult == YES")  
request.sortDescriptors = [SortDescriptor(key: "date", ascending: false)]  
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)

编译器抱怨

"Cannot convert value of type NSFetchedResultsController<NasaPicture> to expected type NSFetchedResultsController<_>"

控制器现在正使用通用类型进行快速处理,但未正确推断实体类型。我已经明确尝试过:

fetchedResultsController = NSFetchedResultsController<NasaPicture>(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)

也没有运气。

谢谢!


阅读 297

收藏
2020-07-07

共1个答案

小编典典

NSFetchRequest现在是通用的。NSFetchedResultsController也是通用的。因此,在声明变量时,必须使用通用声明,例如:

var fetchedResultsController: NSFetchedResultsController<NasaPicture>?
2020-07-07