In my iPad app, I have the following function:
+ (void)addDot:(Dot *)d {
if(dots == nil)
dots = [[NSMutableArray alloc] init];
NSLog(@"before adding, dots = %@",dots);
NSLog(@"adding dot %@",d);
[dots addObject:d];
NSLog(@"dots is now %@",dots);
}
Note that printing a dot results in the x,y coordinates separated by a space.
Everytime the subject taps the screen, a dot is drawn, and this method is called, which adds a dot. Note that dots is defined as static NSMutableArray *dots at the top of the class this function is in. Something weird is going on. Every element of the array is replaced. Look at this output from NSLog at the very beginning after tapping the screen, making 2 dots:
before adding, dots = (
)
2012-02-13 23:58:48.159 MoreMost[520:707] adding dot 418.000000 548.000000
2012-02-13 23:58:48.161 MoreMost[520:707] dots is now (
"418.000000 548.000000"
)
2012-02-13 23:58:48.748 MoreMost[520:707] before adding, dots = (
"635.000000 410.000000"
)
2012-02-13 23:58:48.749 MoreMost[520:707] adding dot 635.000000 410.000000
2012-02-13 23:58:48.750 MoreMost[520:707] dots is now (
"635.000000 410.000000",
"635.000000 410.000000"
)
See how the whole array is being replaced with the incoming element? Why is this? And no, the function is not being called anywhere else in the code. It is only called once, each time the user taps the screen to draw a dot at that location).
Note that the dots are not being used anywhere else in the program. Only this function.
Here is the implementation of Dot:
#import "Dot.h"
@implementation Dot
@synthesize tapPosition;
CGRect frame;
UIColor *dotColor;
float radius = 20;
- (id)initWithTapPosition:(CGPoint)pt color:(UIColor *)col {
if(self = [super init]) {
tapPosition = pt;
float topLeftX = pt.x - radius;
float topLeftY = pt.y - radius;
if(topLeftX + (radius*2) >= 1024)
return nil;
if(topLeftY + (radius*2) >= 728)
return nil;
if(topLeftY <= 0 || topLeftX <= 0)
return nil;
frame = CGRectMake(topLeftX, topLeftY, radius*2, radius*2);
dotColor = col;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
Dot *dot = [[[self class] allocWithZone:zone] initWithTapPosition:tapPosition color:dotColor];
return dot;
}
- (CGRect)getFrame {
return frame;
}
- (UIColor *)getColor {
return dotColor;
}
- (NSString *)description {
return [NSString stringWithFormat:@"%f %f",frame.origin.x,frame.origin.y];
}
@end
and the header:
#import <Foundation/Foundation.h>
@interface Dot : NSObject {
@public
CGPoint tapPosition;
}
@property (nonatomic, assign) CGPoint tapPosition;
- (CGRect)getFrame;
- (id)initWithTapPosition:(CGPoint)pt color:(UIColor *)col;
- (UIColor *)getColor;
@end
Move the
from
@implementationto the@interface:The way you’re declaring them make them actually “global variables”, so all Dot instances will share the same value. Putting them in the
@interfacemake them “instance variables” instead so each Dot can have different values.Old answer
Note that
only adds a reference of the dot
d. If you modify the dot in-place later e.g.then the one in the array will see the same change also.
You need to add a copy instead, e.g.