NSKeyedArchiver & NSKeyedUnarchiver / Swift 3.0
NSKeyedArchiver & NSKeyedUnarchiver / Swift 3.0
我正在使用 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]"
我有谁遇到过这种问题,请告诉我你是如何解决的。
您需要将您的 archiveArray
转换为 Array<Array<Any>>
然后您可以获得包含 Any
类型值的数组数组。
你的解决方案是
if let archiveData = dbRecord.value(forKey: "dataForAppArray") {
if let archiveArray = NSKeyedUnarchiver.unarchiveObject(with: archiveData as! Data) as? Array<Array<Any>> {
firstArray = archiveArray[0] as! [Int]
secondArray = archiveArray[1] as! [CGFloat]
}
}
举个例子加深理解
var firstArray = [Int](), secondArray = [CGFloat]()
firstArray.append(1)
firstArray.append(1)
firstArray.append(1)
secondArray.append(1.1)
secondArray.append(1.2)
secondArray.append(1.3)
print(firstArray) //[1, 1, 1]
print(secondArray) //[1.1, 1.2, 1.3]
let masterArray = [firstArray, secondArray] as [Any] //[[1, 1, 1], [1.1, 1.2, 1.3]]
let dataForApp:NSData = NSKeyedArchiver.archivedData(withRootObject: masterArray) as NSData
现在 unarchiveObject
return Any
然后打印输出以便区分两个输出。
let archiveArray1 = NSKeyedUnarchiver.unarchiveObject(with: dataForApp as Data)
print(archiveArray1!)
输出将是
(
(
1,
1,
1
),
(
"1.1",
"1.2",
"1.3"
)
)
现在将数组转换为 Array<Array<Any>>
if let archiveArray = NSKeyedUnarchiver.unarchiveObject(with: dataForApp as Data) as? Array<Array<Any>> {
print(archiveArray)
}
输出将是
[[1, 1, 1], [1.1, 1.2, 1.3]]
希望你明白我的意思。
我正在使用 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]"
我有谁遇到过这种问题,请告诉我你是如何解决的。
您需要将您的 archiveArray
转换为 Array<Array<Any>>
然后您可以获得包含 Any
类型值的数组数组。
你的解决方案是
if let archiveData = dbRecord.value(forKey: "dataForAppArray") {
if let archiveArray = NSKeyedUnarchiver.unarchiveObject(with: archiveData as! Data) as? Array<Array<Any>> {
firstArray = archiveArray[0] as! [Int]
secondArray = archiveArray[1] as! [CGFloat]
}
}
举个例子加深理解
var firstArray = [Int](), secondArray = [CGFloat]()
firstArray.append(1)
firstArray.append(1)
firstArray.append(1)
secondArray.append(1.1)
secondArray.append(1.2)
secondArray.append(1.3)
print(firstArray) //[1, 1, 1]
print(secondArray) //[1.1, 1.2, 1.3]
let masterArray = [firstArray, secondArray] as [Any] //[[1, 1, 1], [1.1, 1.2, 1.3]]
let dataForApp:NSData = NSKeyedArchiver.archivedData(withRootObject: masterArray) as NSData
现在 unarchiveObject
return Any
然后打印输出以便区分两个输出。
let archiveArray1 = NSKeyedUnarchiver.unarchiveObject(with: dataForApp as Data)
print(archiveArray1!)
输出将是
(
(
1,
1,
1
),
(
"1.1",
"1.2",
"1.3"
)
)
现在将数组转换为 Array<Array<Any>>
if let archiveArray = NSKeyedUnarchiver.unarchiveObject(with: dataForApp as Data) as? Array<Array<Any>> {
print(archiveArray)
}
输出将是
[[1, 1, 1], [1.1, 1.2, 1.3]]
希望你明白我的意思。