NSKeyedArchiver& NSKeyedUnarchiver/Swift 3.0 [英] NSKeyedArchiver & NSKeyedUnarchiver / Swift 3.0

查看:102
本文介绍了NSKeyedArchiver& NSKeyedUnarchiver/Swift 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSKeyedArchiver& NSKeyedUnarchiver将一些复杂的数据存储在Core-Data中,以后再在我的应用中检索. 到目前为止,它一直运行良好,但是在迁移之后,Swift 3.0似乎对我的代码不满意.

I'm using NSKeyedArchiver & NSKeyedUnarchiver to store some complex data in Core-Data and retrieve it later in my app. This was working perfectly until now, but after migration, Swift 3.0 does not seem to be happy with my code.

我已经在代码中提到了这一点:

        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".

以下是检索数据的代码:

Here is how the code to retrieve the data looks like:

    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]
    }

使用代码检索数据时出现问题.它只是在构建时崩溃.

The issue appears whith the code retrieving the data. It simply crashes at build time.

如果我将这两行注释掉:

        //firstArray = (archiveArray as! Array)[0] as [Int]
        //secondArray = (archiveArray as! Array)[1] as [CGFloat]

该程序有效,除了firstArray& (显然)不可用.

The program works, except for the fact that the data in firstArray & is (obviously) not available.

如果我不将其注释掉,则会崩溃,并显示一条很长的消息,并以下面的内容结尾. (我添加了一些...(点)以缩短消息.)

If I do not comment them out, then I get a crash, with a very long message ending with something like what lies below. (I add some ...(dots) to shorten the message.)

.............
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]"

我有人遇到过这种问题,请告诉我您是如何解决的.

I anyone has experienced this kind of problem, please let me know how you solved it.

推荐答案

您需要将archiveArray强制转换为Array<Array<Any>>,然后才能获得包含类型为Any的值的数组.

You need to cast your archiveArray to Array<Array<Any>> then you can get the array of array with contains Any type value.

您的解决方案是

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]
            }

}

让我们举个例子来了解击球手

Let's take an example for batter understanding

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然后打印输出,以便可以区分两个输出.

Now unarchiveObject which return Any then print output so can differentiate both output.

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]]

希望您能理解我的观点.

Hope you understand my point.

这篇关于NSKeyedArchiver&amp; NSKeyedUnarchiver/Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆