Reputation: 59
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;
}
}
Upvotes: 0
Views: 1307
Reputation: 124997
NSLog(@"counter: %@",counter);
You're using the %@
format specifier here, which means that NSLog
expects an object pointer. But you're passing in a NSUInteger
, which is just a number.
If you're getting "(null)" here, it's lucky for you that the array doesn't have any objects in it. If the array had some objects, you'd get a number like 7
back, and interpreting that as a pointer to an object is likely to cause a crash.
The fix is to simply use the right format specifier (%u
) instead:
NSLog(@"counter: %u",counter);
Upvotes: 1
Reputation: 6131
I assume you are referring to the counter
variable? You are assigning the count of matchingLetters
to this variable before adding any elements, so at this point it is of course being assigned 0.
When you later NSLog()
the value of counter
, it will still be 0. There is no automatic update mechanism that would magically update counter with the new count of matchingLetters
once 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)?
Upvotes: 1