2016-12-19 54 views
0

我正在使用NSKeyedArchiver & NSKeyedUnarchiverCore-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]" 

我的人经历了这样的问题,请让我知道你是怎么解决它。

+0

结帐我的回答 –

回答

1

您需要将您的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其返回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]] 

希望你能理解我的观点。

+0

嗨,你的解决方案看起来不错。我需要努力才能使其发挥作用。 我的第一次审判失败。我会随时向你通报。 – Michel

+0

好的谢谢米歇尔 –

+0

你在第一次试验时面临什么样的问题? –