I’m new in Obj C programming, so i will start from beginning
I have an array with some NSNumber and NSString elements, i want to replace NSString elems to NSNumber elems. I decide to make this with for in construction get to new NSMutableArray all elements value and to NSMutableIndexSet their indexes, but when i made this i faced with trouble, here it is, i show you my code and log.
NSArray *oneMoreStack = [program copy];
NSLog(@" stack=%@",oneMoreStack);
NSLog(@" show me index ind=%@",[oneMoreStack indexOfObject:@"x"]);
NSMutableIndexSet *myVarIndexesSet;
NSMutableArray *myVarsArray;
for (id myVarConst in oneMoreStack) {
if([myVarConst isKindOfClass:[NSString class]])
{
if([myVarConst isEqualToString:@"x"]){
[myVarsArray addObject:myVarConst];
NSLog(@"obj x=%@",myVarConst);
[myVarIndexesSet addIndex:[oneMoreStack indexOfObject:myVarConst]];
NSLog(@"ind x=%@",[oneMoreStack indexOfObject:myVarConst]);
}
}
When i run it log show me this:
Calculator[6306:f803] stack=(
x,
2,
"+"
)
Calculator[6306:f803] show me index ind=(null)
Calculator[6306:f803] obj x=x
Calculator[6306:f803] ind x=(null)
then crash [NSMutableArray replaceObjectsAtIndexes:withObjects:]: index set cannot be nil
So, my question is why does [oneMoreStack indexOfObject:myVarConst] return null? How can i fix it? Thanks.
I don’t mention this code before because think problem is in getting indexes.
after previous part of code goes this part:
[stack replaceObjectsAtIndexes:[myVarIndexesSet copy] withObjects:[myVarsArray copy]];
after that crash with error report i mention previously. Thanks.
[oneMoreStack indexOfObject:myVarConst]returns an NSUInteger, a primitive type, not an object.In your NSLog, you are using %@, which says “Hey, take this object I’m passing as an argument, call
-descriptionon it, and print it out”. Luckily, the NSUInteger you are passing in happens to be 0, NSLog thinks that it is a null object, and you aren’t crashing. If the call to -indexOfObject: returns non-0, you will likely crash.Try this instead:
or
You can learn more about string formats at:
Formatting String Objects