【发布时间】:2016-12-19 14:39:48
【问题描述】:
我正在使用NSKeyedArchiver 和NSKeyedUnarchiver 将一些复杂的数据存储在Core-Data 中,稍后在我的应用程序中检索它。
到目前为止,这一直运行良好,但迁移后,Swift 3.0 似乎对我的代码不满意。
我的代码中早有这个:
var firstArray = [Int](), secondArray = [CGFloat]()
.......
// stores some values in firstArray and also in secondArray.
.......
存储数据的代码如下所示:
let masterArray = [firstArray, secondArray] as [Any]
let dataForApp:NSData = NSKeyedArchiver.archivedData(withRootObject: masterArray) as NSData
entityFieldsDico = ["dataForAppArray":dataForApp]
// Use entityFieldsDico to save dataForApp in Core-Data under the key "dataForAppArray".
检索数据的代码如下所示:
if let archiveData = dbRecord.value(forKey: "dataForAppArray") {
let archiveArray = NSKeyedUnarchiver.unarchiveObject(with: archiveData as! Data)
firstArray = (archiveArray as! Array)[0] as [Int]
secondArray = (archiveArray as! Array)[1] as [CGFloat]
}
代码检索数据时出现问题。它只是在构建时崩溃。
如果我注释掉这两行:
//firstArray = (archiveArray as! Array)[0] as [Int]
//secondArray = (archiveArray as! Array)[1] as [CGFloat]
程序可以工作,除了 firstArray & 中的数据(显然)不可用。
如果我不将它们注释掉,那么我会遇到崩溃,并会出现一条很长的消息,结尾类似于下面的内容。 (我添加了一些...(点)来缩短信息。)
.............
0 swift 0x000000010d71fa3d PrintStackTraceSignalHandler(void*) + 45
1 swift 0x000000010d71f466 SignalHandler(int) + 470
2 libsystem_platform.dylib 0x00007fffa0c5a52a _sigtramp + 26
3 libsystem_platform.dylib 0x0000000000000003 _sigtramp + 1597659891
4 swift 0x000000010b25b4e3 swift::constraints::ConstraintGraphScope::~ConstraintGraphScope() + 899
5 swift 0x000000010b2f45f4 swift::constraints::ConstraintSystem::solveSimplified(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 24868
...........
Objects-normal/arm64/UP_ViewController.dia -emit-dependencies-path /Users/me/Library/Developer/Xcode/DerivedData/TheApp-dszaazmmftlmwbicuwcwaplkjdfs/Build/Intermediates/TheApp.build/Debug-iphoneos/TheApp.build/Objects-normal/arm64/UP_ViewController.d -emit-reference-dependencies-path /Users/me/Library/Developer/Xcode/DerivedData/TheApp-dszaazmmftlmwbicuwcwaplkjdfs/Build/Intermediates/TheApp.build/Debug-iphoneos/TheApp.build/Objects-normal/arm64/UP_ViewController.swiftdeps -o /Users/me/Library/Developer/Xcode/DerivedData/TheApp-dszaazmmftlmwbicuwcwaplkjdfs/Build/Intermediates/TheApp.build/Debug-iphoneos/TheApp.build/Objects-normal/arm64/UP_ViewController.o -embed-bitcode-marker
1. While type-checking 'computeFunction' at /Users/me/Documents/iOS/TheApp/TheApp/UP_ViewController.swift:184:5
2. While type-checking expression at [/Users/me/Documents/iOS/TheApp/TheApp/UP_ViewController.swift:235:17 - line:235:66] RangeText="firstArray = (archiveArray as! Array)[0] as [Int]"
我有人遇到过这种问题,请告诉我你是怎么解决的。
【问题讨论】:
-
查看我的答案
标签: ios swift3 nskeyedarchiver nskeyedunarchiver