从NSMutableArray获取对象时,在UITableViewController中获取exc_bad_access

时间:2012-03-21 11:07:22

标签: objective-c automatic-ref-counting

我有一个UITableViewController的子类,我使用另一个自定义类的NSMutableArray初始化子类:

#import <UIKit/UIKit.h>
#import "NUBCheckpointModel.h"

@interface NUBUserCheckpointModel : NSObject

@property (nonatomic,assign) NSString* objId;
@property (nonatomic,assign) NSString* userId;
@property (nonatomic,assign) NSString* checkpointId;
@property (nonatomic,assign) NSDate* dateAdded;
@property (nonatomic,assign) NUBCheckpointModel* checkpoint;

+ (NUBUserCheckpointModel*) fromJson: (NSString*)json;

@end

从另一个ViewController生成的这个数组被传递到这个子类TableViewController,其中包含这个属性

@property (nonatomic,retain) NSMutableArray* userCheckpointData;

此属性设置如下:

- (id)initWithFrame: (CGRect)frame withType: (TableType)typeOfTable fromParent: (UIViewController*)parent data: (NSMutableArray*)ucpData
{
    self = [self init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:frame];
        self.parentController = parent;
        self.userCheckpointData = ucpData;
        [self styleTable];
        [self addPullToRefreshHeader];
        typeCategory = typeOfTable;

    }
    return self;
}

这一部分都很好,任何操作,包括尝试从数组中获取对象都可以正常工作。我测试了它。

我用来测试数组的代码是:

NUBUserCheckpointModel* model = [self.userCheckpointData objectAtIndex:0];
NSLog(model.objId);

但是,这里使用的代码完全相同:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

给我exc_bad_access。我可以知道为什么会这样吗?我似乎无法弄清楚为什么。我正在使用ARC btw。谢谢。

1 个答案:

答案 0 :(得分:1)

添加属性时,需要注意内存管理。对于字符串,设置assign属性不是一个好习惯。

相反,请执行以下操作,

@property (nonatomic,copy) NSString* objId;
@property (nonatomic,copy) NSString* userId;
@property (nonatomic,copy) NSString* checkpointId;
@property (nonatomic,retain) NSDate* dateAdded;
@property (nonatomic,retain) NUBCheckpointModel* checkpoint;
相关问题