How to add objects to NSArray from a different class with ARC
Posted
by
Space Dust
on Stack Overflow
See other posts from Stack Overflow
or by Space Dust
Published on 2012-11-21T16:57:16Z
Indexed on
2012/11/21
16:59 UTC
Read the original article
Hit count: 288
I needed to convert my code to ARC. I have an NSArray that I use to draw a path. I fill the objects of NSArray values from a different class.
Problem is after converting to ARC, NSArray returns always null
I can not see what I am doing wrong.
bug.h
@interface Ladybug : CCSprite <CCTargetedTouchDelegate>{
CCArray *linePathPosition;
}
@property (nonatomic, strong) CCArray *linePathPosition;
@end
bug.m
@synthesize linePathPosition;
-(id) init
{
if( (self=[super init] )) {
self.linePathPosition = [[CCArray alloc] init];
}
return self;
}
-(void) updatePosition:(CGPoint) position
{
[self.linePathPosition addObject:[NSValue valueWithCGPoint:position]];
NSLog(@"line path %@",linePathPosition);
}
-(void) breakMoveLadyBug
{
[self.linePathPosition removeAllObjects];
}
In main .m
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
Ladybug *ladybug1 = (Ladybug *)[self getChildByTag:99];
CCMotionStreak* streak = (CCMotionStreak *)[self getChildByTag:999];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint curPosition = [[CCDirector sharedDirector] convertToGL:touchLocation];
if (ladybug1.isSelected) {
streak.position = curPosition;
[ladybug1 updatePosition:curPosition];
NSLog(@"Cur position %@",NSStringFromCGPoint(curPosition));
if (!ladybug1.isMoving) {
[ladybug1 startMoveLadyBug];
}
}
}
Log:
Cur position {331, 110}
line path (null)
What am I doing wrong? What is the proper way to define and init NSArray with ARC?
© Stack Overflow or respective owner