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访问问题_Objective C_Arrays - Fatal编程技术网

Objective c NSMutableArray访问问题

Objective c NSMutableArray访问问题,objective-c,arrays,Objective C,Arrays,我找了又找不到答案。我已经创建了一个NSMutableArray,在一个访问位置出现了EXC_BAD_访问错误。 在这里这是在.h文件中声明的: NSMutableArray *buttons; ... @property (nonatomic, retain)NSMutableArray *buttons; 这就是合成和实现: @synthesize buttons; ... - (id)init { self = [super init]; if(self != nil)

我找了又找不到答案。我已经创建了一个NSMutableArray,在一个访问位置出现了EXC_BAD_访问错误。 在这里这是在.h文件中声明的:

NSMutableArray *buttons;
...
@property (nonatomic, retain)NSMutableArray *buttons;
这就是合成和实现:

@synthesize buttons;
...
- (id)init {
    self = [super init];
    if(self != nil) {
        buttons = [[NSMutableArray alloc] init];
    }
    return self;
}
...
-(void)addButtonWithImage:(Image*)image {
    Image *button = image;
    [buttons addObject:button];
    [button release];
}
...
-(void)replaceButtonAt:(int)num with:(Image*)image {
    Image *button = image;
    [buttons replaceObjectAtIndex:num withObject:button];  <<===EXC_BAD_ACCESS
    [button release];
}

它可以工作,因为您从不分配、保留、复制等。
按钮
您不应该释放它

只需摆脱
[按钮释放]
s


如果您需要有关引用计数的更多信息,请阅读。

因为您从不分配、保留、复制等。
按钮不应释放它

只需摆脱
[按钮释放]
s


如果您需要有关引用计数的更多信息,则可以阅读。

谢谢,这很有效,但我认为,由于我已将对象存储在数组中,因此它的保留计数为2,我必须将其降低1。或者我只是存储了一个指向对象的指针?您不应该担心保留计数是多少。你应该做的是确保释放你拥有的任何东西。因为你不分配、保留或复制它,所以你就不拥有它<代码>按钮
将保留它,但随后由
按钮
负责释放它。(您有责任释放
按钮
,但是,当您
分配它时)谢谢,这起作用了,但我认为,由于我已将对象存储在数组中,因此它的保留计数为2,我必须将其降低1。或者我只是存储了一个指向对象的指针?您不应该担心保留计数是多少。你应该做的是确保释放你拥有的任何东西。因为你不分配、保留或复制它,所以你就不拥有它<代码>按钮
将保留它,但随后由
按钮
负责释放它。(您有责任释放
按钮
,但是,当您
分配它时)
-(void)renderButton:(int)num atPoint:(CGPoint)point center:(BOOL)center{
    Image *button = [buttons objectAtIndex:num];
    [button renderAtPoint:point centerOfImage:center];
}