简体   繁体   English

目标C将自定义对象添加到NSMutableArray

[英]Objective C Adding custom objects into NSMutableArray

I want to store a list of data records in a NSMutableArray for use in a UITableView. 我想将数据记录列表存储在NSMutableArray中,以便在UITableView中使用。 In other languages I would have used a simple 'type' structure to define the record structure but I understand the way to do this in Obj-C is to define a new class. 在其他语言中,我会使用简单的“类型”结构来定义记录结构,但是我知道在Obj-C中执行此操作的方法是定义一个新类。 I've done this as follows : 我这样做如下:

@interface CustSuppListItem : NSObject
@property (nonatomic, copy, readwrite) NSString *acCode;
@property (nonatomic, copy, readwrite) NSString *acCompany;
@property (nonatomic, copy, readwrite) NSString *acContact;
@property (nonatomic, assign, readwrite) double osBalBase;
@property (nonatomic, assign, readwrite) unsigned int acAccStatus;
@end

@implementation CustSuppListItem
@synthesize acCode, acCompany, acContact, osBalBase, acAccStatus;
@end

In the viewDidLoad of my UITableViewController I instantiate the array : 在我的UITableViewController的viewDidLoad中,我实例化数组:

tableListDataArray = [[NSMutableArray alloc] init];

Once I have retrieved my data, I add it to the array as follows : 检索数据后,将其添加到数组中,如下所示:

CustSuppListItem *custSuppItem = [[CustSuppListItem alloc] init];
[custSuppItem setAcCode:[jsonCustSuppRecord getStringForKey:@"acCode"]];
[custSuppItem setAcCompany:[jsonCustSuppRecord getStringForKey:@"acCompany"]];
[custSuppItem setAcContact:[jsonCustSuppRecord getStringForKey:@"acContact"]];
[custSuppItem setOsBalBase:[jsonCustSuppRecord getDoubleForKey:@"osBalBase"]];
[custSuppItem setAcAccStatus:[jsonCustSuppRecord getIntForKey:@"acAccStatus"]];                             
[tableListDataArray addObject:custSuppItem];                          
[custSuppItem release];

In my table cellForRowAtIndexPath method, I retrieve the data for the current cell as follows: 在我的表格cellForRowAtIndexPath方法中,我按如下方式检索当前单元格的数据:

CustSuppListItem *listDataRecord = [tableListDataArray objectAtIndex:indexPath.row];
[cell.lblCompanyName setText:listDataRecord.acCompany];  // EXC_BAD_ACCESS here
[cell.lblAcCodeContact setText:[NSString stringWithFormat:@"%@, %@",
                                listDataRecord.acCode, listDataRecord.acContact]];
[cell.lblBalance setText:[Utils fmtNumber:listDataRecord.osBalBase withDecPlaces:2]];
[cell.lblStatus setText:[Utils exchAccStatusDesc:listDataRecord.acAccStatus]];
return cell;

In the dealloc method for the view controller I release the NSMutableArray : 在视图控制器的dealloc方法中,我释放NSMutableArray:

[tableListDataArray release];

I'm very new to Obj-C so it would be great if somebody could confirm everything I've done so far makes sense and is in order. 我对Obj-C并不陌生,所以如果有人可以确认我到目前为止所做的一切都有意义并且井井有条,那将是很棒的。 I am getting an intermittent EXC_BAD_ACCESS error when trying to read the acCompany property (see comment next to line) so something must not be right. 尝试读取acCompany属性时,我收到间歇性EXC_BAD_ACCESS错误(请参见行旁的注释),因此某些内容可能不正确。

Any help appreciated, 任何帮助表示赞赏,

Jonathan 乔纳森

All your code looks reasonable and correct to me at first glance. 乍一看,您的所有代码对我来说都是合理且正确的。

A few things that I would look at are: 我要看的几件事是:

  1. Confirm that cell definitely has a property lblCompanyName . 确认单元lblCompanyName定具有属性lblCompanyName If you're trying to assign to a property that doesn't exist then you will get this type of error. 如果您尝试分配给一个不存在的属性,则会出现这种类型的错误。 Have you defined a custom cell object type? 您是否定义了自定义单元对象类型?

  2. Confirm that it is always the acCompany property that is causing the EXC_BAD_ACCESS , and not just any property on the object. 确认始终是导致EXC_BAD_ACCESSacCompany属性,而不是对象上的任何属性。 One way to do this would be to change the ordering of the lines in the cellForRowAtIndexPath method. 一种方法是更改cellForRowAtIndexPath方法中各行的顺序。

  3. Confirm that the listDataRecord that's causing the crash is getting populated correctly in the first place. 确认首先正确填充了导致崩溃的listDataRecord。 In other words, confirm that your jsonCustSuppRecord is always valid. 换句话说,请确认您的jsonCustSuppRecord始终有效。 What does jsonCustSuppRecord getStringForKey: return if the key doesn't exist in the jsonCustSuppRecord ? 如果密钥在jsonCustSuppRecord中不存在,则jsonCustSuppRecord getStringForKey:返回什么?

  4. Set a breakpoint at this line: [tableListDataArray addObject:custSuppItem]; 在此行设置一个断点: [tableListDataArray addObject:custSuppItem]; and examine the contents of the custSuppItem each time (this is an extension of point 3. above) 并每次检查custSuppItem的内容(这是上面第3点的扩展)

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

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