3中初始化NSFetchedResultsController

3中初始化NSFetchedResultsController

本文介绍了如何在iOS 10 Swift 3中初始化NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Swift 3在iOS 10中初始化NSFetchedResultsController。

I can't get my NSFetchedResultsController initialized in iOS 10 using 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)

编译器抱怨

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

控制器现在使用swift的通用类型,但它不会正确推断实体类型。我已明确尝试:

The controller is now using generic type for swift, but it is not inferring the entity type correctly. I've tried explicitly:

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

也不幸。

谢谢!

推荐答案

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

NSFetchRequest is now a generic. NSFetchedResultsController is a generic too. Therefore, when declaring variables, you have to use a generic declaration, e.g.:

var fetchedResultsController: NSFetchedResultsController<NasaPicture>?

这篇关于如何在iOS 10 Swift 3中初始化NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:59