Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从NSMutableArray获取对象时在UITableViewController中获取exc\u bad\u访问权限_Objective C_Automatic Ref Counting - Fatal编程技术网

Objective c 从NSMutableArray获取对象时在UITableViewController中获取exc\u bad\u访问权限

Objective c 从NSMutableArray获取对象时在UITableViewController中获取exc\u bad\u访问权限,objective-c,automatic-ref-counting,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) NSStrin

我有一个子类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
此属性设置如下:

- (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\u坏访问权限。我能知道为什么会这样吗?我似乎不明白为什么。我正在使用ARC btw。谢谢。

添加属性时,您需要注意内存管理。对于字符串,设置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;
@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;