NSKeyedArchiver.archivedData崩溃对象的NSDictionary

时间:2017-02-23 00:42:53

标签: swift3

在将应用程序从Objective C转换为Swift 3时,许多数据结构不再是类,而是结构。在一个特定的函数中,我需要使用NSKeyedArchiver.archivedData(withRootObject:function来封装这些结构,然后通过网络连接发送它们,所以我将它们转换为旧的Objective-C对象。但下面的代码仍然当我尝试对它们进行编码时崩溃。

enum CustomDataType:Int
{
    case CustomDataType0 = 0
    case CustomDataType1 = 1
    case CustomDataType2 = 2
    case CustomDataType3 = 3
    case CustomDataType4 = 4
}

func send(dataType:CustomDataType, dictionary: Dictionary<String, Any>)
{
   // convert everything into objects
    let dataTypeObject:NSNumber = NSNumber(integerLiteral: dataType.rawValue)
    let dataValueObject:NSDictionary = dictionary as NSDictionary
    let dataTypeObjectKey:NSString = "regis-type"
    let dataValueObjectKey:NSString = "regis-value"

    let transmissionData:NSDictionary = [dataTypeObjectKey : dataTypeObject, dataValueObjectKey :dataValueObject]

    let data = NSKeyedArchiver.archivedData(withRootObject: transmissionData)

最后一行产生了崩溃,我感到很困惑。有什么想法吗?

崩溃日志:

[_ SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例0x170448df0 2017-02-22 17:10:10.338319 cd Swift [441:56841] ***由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; - [_ SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例0x170448df0&#39;

0 个答案:

没有答案