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

Swift: Unable to infer complex closure type with NSFetchedResultsController

将我的项目升级到 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"

经过一些阅读,我了解到 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!