NSMutableArray out of scope problem
I'm new to objective-C. I want to use NSMutableArray to store some objects i will access later.
I've got the interface:
@interface ControlWheel : RotatableObject <LeverSubject>
{
NSMutableArray *subjectArray_;
}
-(id) initWithCWDef: (ControlWheelDef*) def;
-(void) addSubject: (id) subject;
@end
Here is the implementation:
-(id) initWithCWDef:(ControlWheelDef *)def
{
...
self = [super initWithDef:&rDef];
if (self)
{
subjectArray_ = [NSMutableArray arrayWithCapacity:1];
开发者_开发技巧 }
return self;
}
-(void) addSubject:(id)subject
{
[subjectArray_ addObject:subject];
}
-(void) angleChangeCallback
{
unsigned int count = [subjectArray_ count];
for (unsigned int i = 0; i < count; i++)
{
[[subjectArray_ objectAtIndex:i] onAngleChanged:angle_];
}
}
The problem is in angleChangeCallback function. subjectArray_ is out of scope. What i'm doing wrong?
arrayWithCapacity:
returns an autoreleased array. You want [[NSMutableArray alloc] init];
.
NSMutableArray
grows as necessary, so arrayWithCapacity:
isn't particularly useful, most of the time, unless you are going to fill it with a known, large number of objects.
Oh, and it's not gone out of scope; it's been collected by the autorelease pool.
It doesn't look like you have angleChangeCallback defined in your interface at the top. I see addSubject and initWithCWDef but not him.
精彩评论