Can anyone tell me why [NSMutableArray count] is always returning (null) in the debug window? It is initialized, the matching letters are registering, but it still does not return anything but null.
@implementation NSString (Word)
-(NSMutableArray *)placeOfLetter: (char)letterAsked;{
NSMutableArray *matchingLetters;
matchingLetters=[[NSMutableArray alloc]init];
int len=(int)[self length];
NSLog(@"length of word: %i, letterAsked=%c",len,letterAsked);
NSUInteger counter=[matchingLetters count];
for (NSUInteger x=0; x<[self length]; x++) {
if ([self characterAtIndex:x]==letterAsked){
NSNumber *foundPosition = [NSNumber numberWithUnsignedInteger:x];
[matchingLetters addObject:foundPosition];
NSLog(@"found at place : %@",foundPosition);
NSLog(@"counter: %@",counter);
}
}
if ([matchingLetters count]<=0){
NSLog(@"counter: %@",counter);
NSLog(@"no letters ");
return nil;
}else{
return matchingLetters;
}
}
I assume you are referring to the
countervariable? You are assigning the count ofmatchingLettersto this variable before adding any elements, so at this point it is of course being assigned 0.When you later
NSLog()the value ofcounter, it will still be 0. There is no automatic update mechanism that would magically update counter with the new count ofmatchingLettersonce you’ve added elements to this array. And it displays as (null) because you’re using%@instead of%qu.If that’s not what you’re referring to, are you perhaps referring to breaking your application in the debugger and then entering
p -[matchingLetters count]? Or are you referring to part of the variables display on the left side of the debugging console (if so, a screenshot of what you’re seeing might be helpful)?