I am encountering a weird issue that I couldn't find the bug of the testing code that I wrote. I am writing a piece of code to generate AttributedString and put it in NSMutableArray, but when I display the added objects from the NSMutableArray using the For loop, the output is showing the same object with same index 3 times, I am trying to debug it for 2 days now and couldn't find the bug, hopefully I can get some help/advice here.
The code to instantiate the NSMutableArray and adding the AttributedString to the array
-(instancetype)init
{
self = [super init];
if (self) {
for (NSMutableAttributedString *attrString in [SetPlayingCard symbolArray]) {
for (NSString *attrColor in [SetPlayingCard colorsArray]) {
if ([attrColor isEqualToString:@"redColor"]) {
[attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
NSStrokeColorAttributeName : [UIColor redColor]}
range:NSMakeRange(0, [attrString length])];
NSLog(@"%@ %@",attrColor, attrString);
} else if ([attrColor isEqualToString:@"blueColor"]){
[attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
NSStrokeColorAttributeName : [UIColor blueColor]}
range:NSMakeRange(0, [attrString length])];
NSLog(@"%@ %@",attrColor, attrString);
} else if ([attrColor isEqualToString:@"purpleColor"]){
[attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
NSStrokeColorAttributeName : [UIColor purpleColor]}
range:NSMakeRange(0, [attrString length])];
NSLog(@"%@ %@",attrColor, attrString);
}
[self addCard:attrString];
}
}
}
return self;
}
The Class method that is being called
+ (NSArray *)colorsArray
{
return @[@"redColor",@"blueColor",@"purpleColor"];
}
+ (NSArray *)symbolArray
{
NSMutableAttributedString *triangle = [[NSMutableAttributedString alloc] initWithString:@"Triangle"];
NSMutableAttributedString *square = [[NSMutableAttributedString alloc] initWithString:@"Square"];
NSMutableAttributedString *round = [[NSMutableAttributedString alloc] initWithString:@"Round"];
return @[triangle,square,round];
}
The method to add and display the output
- (void)addCard:(NSAttributedString *)card
{
NSLog(@"String to be added to array: %@", card);
[self.cards addObject:card];
NSLog(@"Index is %d for %@", [self.cards indexOfObject:card], card);
}
- (NSAttributedString *)printCard
{
NSAttributedString *card;
if ([self.cards count]) {
for (card in self.cards) {
NSLog(@"Count: %d, Array index: %d, Card from the deck is: %@, ", [self.cards count], [self.cards indexOfObject:card], card);
}
}
return card;
}
The output I get from the NSLog in addCard method is as below.
2014-04-01 22:40:56.184 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.185 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.186 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.187 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.188 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.189 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.190 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.191 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
2014-04-01 22:40:56.192 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
NSStrokeWidth = 3;
},
indexOfObjectwill give you the first index of an object thatisEqual. it doesn't do address comparison. you probably want to useindexOfObjectIdenticalTo