Ios 将NSKeyArchiver从Obj C转换为Swift 3

Ios 将NSKeyArchiver从Obj C转换为Swift 3,ios,objective-c,xcode,swift3,nskeyedarchiver,Ios,Objective C,Xcode,Swift3,Nskeyedarchiver,我有一个在Obj C中创建并使用以下方法存档的类 // encode the history object for saving NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject]; 现在我想在一个完全不同的应用程序中解构同一个类。我能够访问保存这些归档对象的阵列,但我很难将它们解除归档 在Obj C中,我使用了这种方法 // un archive the h

我有一个在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
    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
更新

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

> print("\(type(of: statementHistory)) type of array") // Array<UserDefaultHistory> type of array
我得到这个错误

NSFastEnumerationIterator.Element' (aka 'Any') is not convertible to 'NSData'
enter code here
    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_owe_history object这个项目名为userDefaultHistory我想你要做的就是先将NSKeyedUnarchiver的结果保存为数据,并指定一个类型。@El Tomato你能告诉我你的意思吗,我不确定我是否明白你的意思。如果
historyData
不是
NSData
这行代码会导致运行时崩溃-
让tempData=historyData为!NSData
这不是NSData吗<代码>NSData*HisOtryEncodeObject=[NSKeyedArchivedDatawithRootObject:historyObject]
 for historyData in historyArray{
    let tempData = historyData as! NSData
    let historyObject = UserDefaultHistory
    historyObject = NSKeyedUnarchiver.unarchiveObject(with: tempData)
    print("\(historyObject.saveDate)")
}