我正在使用NSKeyedArchiver
& NSKeyedUnarchiver
在Core-Data
中存儲一些複雜數據,稍後在我的應用中檢索它。 這直到現在都完美運行,但在遷移之後,Swift 3.0
似乎並不滿意我的代碼。NSKeyedArchiver&NSKeyedUnarchiver/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]
}
問題出現在代碼檢索數據。它只是在構建時崩潰。
如果我註釋掉那些2線:
//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]"
我的人經歷了這樣的問題,請讓我知道你是怎麼解決它。
結帳我的回答 –