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

Objective c 目标C将自定义对象添加到NSMutableArray中,objective-c,object,nsmutablearray,exc-bad-access,Objective C,Object,Nsmutablearray,Exc Bad Access,我想将数据记录列表存储在NSMutableArray中,以便在UITableView中使用。在其他语言中,我会使用一个简单的“类型”结构来定义记录结构,但我知道在Obj-C中这样做的方法是定义一个新类。我的做法如下: @interface CustSuppListItem : NSObject @property (nonatomic, copy, readwrite) NSString *acCode; @property (nonatomic, copy, readwrite) NSStri

我想将数据记录列表存储在NSMutableArray中,以便在UITableView中使用。在其他语言中,我会使用一个简单的“类型”结构来定义记录结构,但我知道在Obj-C中这样做的方法是定义一个新类。我的做法如下:

@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
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];
在UITableViewController的viewDidLoad中,我实例化了数组:

tableListDataArray = [[NSMutableArray alloc] init];
检索完数据后,我将其添加到数组中,如下所示:

@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
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];
在我的表格cellforrowatinexpath方法中,我检索当前单元格的数据,如下所示:

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;
在视图控制器的dealloc方法中,我释放NSMutableArray:

[tableListDataArray release];
我是Obj-C的新手,所以如果有人能确认我迄今为止所做的一切都是合理的和有序的,那就太好了。我在尝试读取伴随属性时遇到间歇性EXC_BAD_访问错误,请参见第行旁边的注释,因此某些内容一定不正确

感谢您的帮助


乔纳森(Jonathan)

乍一看,你所有的代码都是合理和正确的

我要看的几件事是:

确认单元格确实具有lblCompanyName属性。如果您试图分配给一个不存在的属性,那么您将得到这种类型的错误。是否定义了自定义单元对象类型

确认总是伴随属性导致EXC_BAD_访问,而不仅仅是对象上的任何属性。一种方法是在CellForRowatineXpath方法中更改行的顺序

首先确认导致崩溃的listDataRecord已正确填充。换句话说,请确认您的JSONCustomSuppRecord始终有效。如果键在JSONCustomSuppRecord中不存在,JSONCustomSuppRecord getStringForKey:返回什么

在此行设置断点:[tableListDataArray addObject:custSuppItem];并在每次作为第3点的扩展时检查custSuppItem的内容。在上面


您是否在viewDidLoad中保留了tableListDataArray?当出现EXC\u BAD\u访问错误时,控制台中还有其他详细信息吗?另外,将CustSuppListItem实体封装为类的设计是非常典型的ObjC设计。@vpdn为什么保留?init返回重新计数为的对象+1@alanduncantableListDataArray在viewDidLoad中创建,然后在cellForRowAtIndexPath中使用。因为他似乎没有为它使用属性,我只是想知道他是否在他的viewDidLoad方法中手动保留了数组。我目前在实例化tableListDataArray时没有使用-retain选项,我想我不需要一个?该变量在头文件中声明:@interface CustSuppListView:UITableViewController{NSMutableArray*tableListDataArray;}@Jonathan-为了让其他人可以从中学习,问题/解决方案是什么?