【发布时间】:2014-05-14 18:33:57
【问题描述】:
我创建了一个自定义对象 (historyObject),其中包含一些属性。其中之一是名为 savedDate 的 NSString。我创建了自定义对象,设置了我需要保存的所有值并将其放在一个数组 (historyArray) 中。在另一个 VC 中,我正在使用一个表格视图,并希望使用自定义对象属性 savedDate 填充表格视图单元格 textLable。
我可以 NSLog 退出 historyArray,所以我知道对象在那里,我可以看到它们的名称,但我发现很难访问该对象的属性。
这就是我试图用来设置一个 NSString 以供以后用于单元格 lableText 的内容:
NSString *cellLableText = [billDetails.historyArray objectAtIndex:indexPath.row] saveDate;
我觉得我在看一些非常简单的东西,但看不出来。
************************************************ ** 更新 *********
在经历了更多的尝试和错误之后,我开始怀疑我是否正确地对对象进行了编码。创建历史对象时,我在历史对象上调用此方法
NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject];
而且历史对象符合 NSCoding,所以我相信我是正确的。
然后,当我想在稍后使用对象中的数据时,我正在尝试这样做: // 获取历史数组
historyArray = [[NSArray alloc] initWithArray:billDetails.historyArray];
然后我像这样取消归档 nsData:
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);
// gives me back this: This is the history object <GD_Owed_HistoryObject: 0xb953400>
}
但是这个循环似乎只是给我回了内存位置?
for (id obj in historyArray){
NSLog(@"obj: %@", obj);
}
返回的内容:obj: <62706c69 73743030 d4010203 0405081d 1e542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a6090a13 14151655 246e756c 6cd40b0c 0d0e0f10 11125c6f 77656453 61766544 6174655f 10116f77 6564416d 6f756e74 4368616e 6765645f 100f6f77 6564546f 74616c41 6d6f756e 74562463 6c617373 80028003 80048005 5a30352f 31392f32 30313456 2435302e 30305724 3135302e 3030d217 18191c58 24636c61 73736573 5a24636c 6173736e 616d65a2 1a1b5f10 1547445f 4f776564 5f486973 746f7279 4f626a65 6374584e 534f626a 6563745f 10154744 5f4f7765 645f4869 73746f72 794f626a 65637412 000186a0 5f100f4e 534b6579 65644172 63686976 65720008 00110016 001f0028 00320035 003a003c 00430049 0052005f 00730085 008c008e 00900092 0094009f 00a600ae 00b300bc 00c700ca 00e200eb 01030108 00000000 00000201 00000000 0000001f 00000000 00000000 00000000 0000011a>
当我尝试访问这样的属性时,它会崩溃 [[historyArray objectAtIndex:indexPath.row] saveDate];
给我这个错误:
由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'-[NSConcreteMutableData saveDate]: 无法识别的选择器发送到实例 0xb941460'
我想我的问题是我是否正确编码和解码对象?
编辑: 因此,经过多次试验和错误后,我通过解码保存的对象然后将该对象添加到新数组并使用该新数组填充我的表格视图单元格来使其工作。
感谢您的帮助!
【问题讨论】:
-
当您尝试您的代码时会发生什么?你有错误吗? cellLableText 是 nil 还是意外值?等等
标签: ios objective-c encoding nsarray nsobject