本文介绍了无法在Swift 3中实例化带有通用类型AnyObject的NSFetchedResultController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! NSFetchedResultsController需要一个通用的类型参数,并且需要一个通用的类型参数。 AnyObject已经工作正常到现在。编译器抛出错误: 类型AnyObject不符合协议'NSFetchRequestObject' 让我额外混淆,如果你删除类型参数,XCode然后说: 引用通用类型NSFetchedResultsController需要参数` 。>` 并帮助建议使用< AnyObject& /code>,循环重复。 这看起来非常像一个错误。 如果你看看 NSFetchedResultsController c>,可以清楚地看到它有一个名为 ResultType 的参数,它符合 NSFetchRequestResult 。所以你应该传递符合 NSFetchRequestResult 的类型。 因此,如果您查看 NSFetchRequestResult ,您可以看到它符合 NSObjectProtocol 。另外 NSDictionary , NSManagedObject 和 NSManagedObjectID 符合 NSFetchRequestResult 。 public protocol NSFetchRequestResult:NSObjectProtocol {} 扩展NSDictionary:NSFetchRequestResult {} 扩展NSManagedObject:NSFetchRequestResult {} 扩展NSManagedObjectID:NSFetchRequestResult { } 所以很清楚,你应该传递一个类型从这三个 NSDictionary 或 NSManagedObject 或 NSManagedObjectID 。 像这样创建 NSFetchedResultsController 的实例。 let resultsController:NSFetchedResultsController< NSManagedObject> ;! 或像这样 let resultsController:NSFetchedResultsController< NSManagedObjectID> ;! 或像这样 let resultsController:NSFetchedResultsController< NSDictionary> ;! I'm experimenting with CoreData in Swift 3 and have come up against a very bizarre circular compiler error in Xcode 8 beta.NSFetchedResultsController needs a generic type parameter and AnyObject has worked fine up until now. The compiler throws the error: Type 'AnyObject' does not conform to protocol 'NSFetchRequestObject'To make me extra confused, if you delete the type parameter, XCode then says:Reference to generic type NSFetchedResultsController requires argument in `<...>`and helpfully suggests a fix using <AnyObject>....and the cycle repeats.This looks very much like a bug. Any ideas before I report it? 解决方案 If you take a look into NSFetchedResultsController, you can clearly see that it has a parameter with name ResultType which conforms to NSFetchRequestResult. So you should pass a type which conforms to NSFetchRequestResult.So if you take a look into NSFetchRequestResult, you can see that it conforms to NSObjectProtocol. Also NSDictionary, NSManagedObject and NSManagedObjectID conforms to NSFetchRequestResult.public protocol NSFetchRequestResult : NSObjectProtocol {}extension NSDictionary : NSFetchRequestResult {}extension NSManagedObject : NSFetchRequestResult {}extension NSManagedObjectID : NSFetchRequestResult {}So it clear that you should pass a type from any of these three NSDictionary or NSManagedObject or NSManagedObjectID.Create your instance of NSFetchedResultsController like this.let resultsController : NSFetchedResultsController<NSManagedObject>!or like this let resultsController : NSFetchedResultsController<NSManagedObjectID>!or like thislet resultsController : NSFetchedResultsController<NSDictionary>! 这篇关于无法在Swift 3中实例化带有通用类型AnyObject的NSFetchedResultController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-15 08:26