我有我的OBJ的C創建並使用以下梅託德從obj獲得Ç轉換NSKeyArchiver斯威夫特3
// encode the history object for saving
NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject];
現在我想解除封存的是同一類存檔,但在一個完全不同的應用程序的類。我可以訪問保存這些存檔對象的數組,但我很難解除它們的存檔。
在的OBJ C I用這個方法
// 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];
}
我試圖將其轉換成迅速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Ç應用歸檔對象。
我得到這個錯誤
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
我只是在茫然,是哪些因素NSArray如果不是UserDefaultHistory對象。
什麼是UserDefaultHistory,爲什麼要將對象從unarchiveObject設置爲UserDefaultHistory? –
userDefaultHistory是用於解碼對象的NSCoding類。我的舊項目被稱爲GD_Owed_HistoryObject這個項目其名爲userDefaultHistory – icekomo
我認爲你所要做的就是將NSKeyedUnarchiver的結果作爲數據第一次保存,指定一個類型。 –