Swift 3:无法使用NSFetchedResultsController推断复杂的闭包类型

时间:2016-08-30 23:07:07

标签: migration nsfetchedresultscontroller swift3 ios10 xcode8

将项目升级到Swift 3后,以下初始化程序不再构建:

1    var fetchedResultsController: NSFetchedResultsController {
2        if _fetchedResultsController != nil {
3            return _fetchedResultsController!
4        }
5        
6        let fetchRequest: NSFetchRequest = MyEntity.fetchRequest()
...

错误发生在第1行:

"Unable to infer complex closure return type; add explicit type to disambiguate"

第6行给出了进一步的错误:

"Generic parameter 'MyEntity' could not be inferred"

1 个答案:

答案 0 :(得分:18)

经过一些阅读后,我了解到NSFetchRequestNSFetchedResultsController现在在iOS 10中是通用的,Apple建议明确指定其类型:

1    var fetchedResultsController: NSFetchedResultsController<MyEntity> {
2        if _fetchedResultsController != nil {
3            return _fetchedResultsController!
4        }
5        
6        let fetchRequest: NSFetchRequest<MyEntity> = MyEntity.fetchRequest()
...

一个有用的提示(针对Swift 3迁移中的这个和其他问题)只是在XCode中从模板创建一个新的应用程序!