【问题标题】:Converting NSKeyArchiver from Obj C to Swift 3将 NSKeyArchiver 从 Obj C 转换为 Swift 3
【发布时间】:2017-07-21 15:52:23
【问题描述】:

我有一个在 Obj C 中创建并使用以下方法归档的类

    // encode the history object for saving
NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject];

现在我想在一个完全不同的应用程序中取消归档同一个课程。我能够访问保存这些归档对象的数组,但我无法取消归档它们。

在 Obj C 中我使用了这个方法

// un archive the history array objects
for (NSData *historyData in historyArray)
{
    // set a instance of the person class to each NSData object found in the temp array
    GD_Owed_HistoryObject *historyObject = [[GD_Owed_HistoryObject alloc] init];
    historyObject = [NSKeyedUnarchiver unarchiveObjectWithData:historyData];
    NSLog(@"This is the history object %@", historyObject);
    NSLog(@"This is the save date %@", historyObject.saveDate);
    NSLog(@"This is the total before %@", historyObject.beforeTotal);
    NSLog(@"This is the total after %@", historyObject.afterTotal);
    NSLog(@"This is the amount changed %@", historyObject.amountChanged);
    //[decodedHistoryArray addObject: historyObject];

    [decodedHistoryArray insertObject:historyObject atIndex:0];
}

我尝试将其转换为 swift 3

let historyArray = NSArray(array:statementHistory, copyItems: true)

    for historyData:NSData in historyArray{

        let historyObject = UserDefaultHistory
        historyObject = NSKeyedUnarchiver.unarchiveObject(with: historyData)
        print("\(historyObject.saveDate)")
    }

historyArray 包含来自 Obj C 应用程序的存档对象。

我收到此错误

NSFastEnumerationIterator.Element' (aka 'Any') is not convertible to 'NSData'
enter code here

更新

当我检查数组中的对象时,我得到了我期望的结果。

> print("\(type(of: statementHistory)) type of array") // Array<UserDefaultHistory> type of array

但是当我尝试像这样访问 UserDefaultHistory 的属性时

print("\(type(of: statementHistory[0].saveDate)) this is the save date")

我收到此错误

    fatal error: NSArray element failed to match the Swift Array Element type
2017-03-02 15:01:59.098064 owed[1535:708223] fatal error: NSArray element failed to match the Swift Array Element type

如果不是 UserDefaultHistory 对象,我不知道 NSArray 中的元素是什么。

【问题讨论】:

  • 什么是 UserDefaultHistory,为什么要将对象从 unarchiveObject 设置为 UserDefaultHistory?
  • userDefaultHistory 是用于解码对象的 NSCoding 类。我的旧项目叫做 GD_Owed_HistoryObject 这个项目叫做 userDefaultHistory
  • 我想你要做的就是先把NSKeyedUnarchiver的结果保存为数据,指定一个类型。
  • @El Tomato 你能告诉我你的意思吗,我不确定我是否听懂了你的意思。

标签: ios objective-c xcode swift3 nskeyedarchiver


【解决方案1】:
 for historyData in historyArray{
    let tempData = historyData as! NSData
    let historyObject = UserDefaultHistory
    historyObject = NSKeyedUnarchiver.unarchiveObject(with: tempData)
    print("\(historyObject.saveDate)")
}

【讨论】:

  • 如果 historyData 不是 NSData 此行会导致运行时崩溃 - let tempData = historyData as! NSData
  • 这不是 NSData 吗? NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多