简体   繁体   中英

Objective C Adding custom objects into NSMutableArray

I want to store a list of data records in a NSMutableArray for use in a 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. 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 :

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:

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 :

[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. 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.

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 . 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. One way to do this would be to change the ordering of the lines in the cellForRowAtIndexPath method.

  3. Confirm that the listDataRecord that's causing the crash is getting populated correctly in the first place. In other words, confirm that your jsonCustSuppRecord is always valid. What does jsonCustSuppRecord getStringForKey: return if the key doesn't exist in the jsonCustSuppRecord ?

  4. Set a breakpoint at this line: [tableListDataArray addObject:custSuppItem]; and examine the contents of the custSuppItem each time (this is an extension of point 3. above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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