【问题标题】:Access a property from a custom object inside a NSArray从 NSArray 中的自定义对象访问属性
【发布时间】: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: &lt;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&gt;

当我尝试访问这样的属性时,它会崩溃 [[historyArray objectAtIndex:indexPath.row] saveDate];

给我这个错误:

由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'-[NSConcreteMutableData saveDate]: 无法识别的选择器发送到实例 0xb941460'

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

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

感谢您的帮助!

【问题讨论】:

  • 当您尝试您的代码时会发生什么?你有错误吗? cellLableText 是 nil 还是意外值?等等

标签: ios objective-c encoding nsarray nsobject


【解决方案1】:

你需要用括号来访问你的historyObject的对象。

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

【讨论】:

  • 我觉得[billDetails.historyArray[indexPath.row] saveData]; 更容易阅读。
  • 如果我想注销该对象的内容,有什么好方法?循环?我想我记得 Obj C 有一些方便的方法来处理这类事情。
  • @rmaddy 问题在于不知道如何访问对象属性的人随后会暴露于点语法和文字,他们更感谢他们可能还不知道。无论如何,每个人的编码风格都是不同的。我不喜欢点语法和文字。
  • @icekomo 取决于你的意思。如果你想注销historyObject的所有内容,你需要重写-(NSString *)description函数,然后你可以做NSLog(@"%@", historyObject)
  • @icekomo 如果你只是想注销每个对象,你可以这样做for(HistoryObject *h in historyArray) NSLog(@"%@", h);
【解决方案2】:

使用强制转换查看对象的属性

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

【讨论】:

    【解决方案3】:

    比在任何地方都需要在索引处访问对象更好的方法是像这样向类添加一个方法...

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

    那么你可以这样做......

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

    【讨论】:

      【解决方案4】:

      saveDate(非原子,强)吗?

      如果是弱属性并且你使用了 ARC,你的对象将返回 nil;

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

      【讨论】:

      • 保存日期是这样的:@property (nonatomic, strong) NSString *saveDate;是的,不,它并不弱。
      • 仅供参考:通常最好将NSString 属性设为copy 而不是strong,除非您准备好处理NSMutableStrings
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 2011-11-09
      • 2019-05-03
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多