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

Jas*_*lin 16 core-data ios swift

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

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)  
Run Code Online (Sandbox Code Playgroud)

编译器抱怨说

"Cannot convert value of type NSFetchedResultsController<NasaPicture> to expected type NSFetchedResultsController<_>"
Run Code Online (Sandbox Code Playgroud)

控制器现在使用swift的泛型类型,但它没有正确地推断实体类型.我已经明确尝试过:

fetchedResultsController = NSFetchedResultsController<NasaPicture>(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)  
Run Code Online (Sandbox Code Playgroud)

也没有运气.

谢谢!

Sul*_*han 24

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

var fetchedResultsController: NSFetchedResultsController<NasaPicture>?
Run Code Online (Sandbox Code Playgroud)

  • @JasonC.Howlin你能具体说明你做了什么吗?我有一段时间懒洋洋地宣布一个frc,指定一个谓词,排序描述符等. (2认同)