我正试图在核心数据项目(Swift)上实现状态恢复,并且在分屏视图控制器中实现UIDataSourceModelAssociation协议的UITableView数据源上存在问题,这是一个围绕NSFetchedResultsController的包装类。 代码是:
1 extension EventDataProvider : UIDataSourceModelAssociation
2 {
3 public func modelIdentifierForElement(at idx: IndexPath, in view: UIView) -> String?
4 {
5 let elementAtIndexPath = self.fetchedResultsController.object(at: idx)
6
7 return String(describing: elementAtIndexPath.objectID.uriRepresentation())
8 }
9 public func indexPathForElement(withModelIdentifier identifier: String, in view: UIView) -> IndexPath?
10 {
11 if let url = URL(string: identifier),
12 let objectID = self.fetchedResultsController.managedObjectContext.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: url),
13 let object = self.fetchedResultsController.managedObjectContext.object(with: objectID) as? CDEvent
14 {
15 return self.fetchedResultsController.indexPath(forObject: object) as NSIndexPath?
16 }
17
18 return nil
19 }
20 }
我得到一个EXC_BAD_INSTRUCTION异常,它在AppDelegate类的顶部停止调试器,状态恢复似乎指向"静态Foundation.IndexPath"的问题。_unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath".
我使用restorationArchiveTool来转换结果数据。使用命令"…"将savedState文件夹中的数据文件保存到一个列表中。/restorationArchiveTool——plist——structured -o path/to/outputfile
如果我使用Preview查看生成的plist文件,我将得到以下内容:
kApplicationSelectedCellIndexPathsKey……("
…但如果我在Xcode中打开plist,我得到以下内容:
kApplicationSelectedCellIndexPathsKey用于键,但仅(用于值
)假设解码器使用与plist阅读器相同的算法来转换数据文件,那么得到某种异常就不足为奇了。
如果我删除UIDataSourceModelAssociation扩展,异常就会消失。
还有人能确认这个问题吗?还是我错过了一些非常明显的东西?
你的函数有签名:
indexPathForElement(withModelIdentifier identifier: String, in view: UIView) -> IndexPath?
这就是为什么你应该将返回类型转换为IndexPath
而不是NSIndexPath
:
return self.fetchedResultsController.indexPath(forObject: object) as NSIndexPath?
return self.fetchedResultsController.indexPath(forObject: object) as IndexPath?