繁体   English   中英

从NSArray内的自定义对象访问属性

[英]Access a property from a custom object inside a NSArray

我有一个用几个属性创建的自定义对象(historyObject)。 其中之一是名称为saveDate的NSString。 我创建了自定义对象集,将所有需要保存的值保存并将其放置在数组(historyArray)中。 从另一个VC,我正在使用tableview,并希望使用自定义对象属性saveDate填充table view单元格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”

我想我的问题是我是否正确编码和解码对象?

编辑:因此,在经过更多尝试和错误后,我通过解码保存的对象,然后将该对象添加到新数组中,并使用该新数组填充表视图单元格,使其开始工作。

谢谢您的帮助!

您需要使用方括号括起来才能访问historyObject的对象。

NSString *cellLabelText = [[[billDetails historyArray] objectAtIndex:[indexPath row]] saveDate];

使用转换查看对象的属性

((HistoryObject*)billDetails.historyArray[indexPath.row]).saveData 

与在需要访问索引的对象处进行强制转换相比,一种更好的方法是像这样向类添加方法。

- (HistoryItem *)historyItemAtIndex:(NSUInteger)index {
    return self.billDetails.historyArray[index];
}

那么你可以做到这一点...

NSString *cellLabelText = [self historyItemAtIndex:indexPath.row].saveDate;

是saveDate(非原子的,强壮的)吗?

如果属性较弱而您使用ARC,则对象将返回nil;

NSString *text = [[[billDetails historyArray] objectAtIndex:[indexPath row]] saveDate];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM