1
int num1, num2,num3,num4, i=0;
NSMutableArray *charArray = [[NSMutableArray alloc] init];

while(num1 != 0 && num2 != 0) {
  num3 = num1 & 1;
  num4 = num2 & 1;
  if(num3 != num4) {
    if(num3 == 0) {
      charArray[i++]= '0';
      charArray[i++]= '1';  
    } else {
      charArray[i++]='1';
      charArray[i++]='0';   
    }
  }
  num1 = num1 > 1;
  num2 = num2 > 1;
}

}

I am kinda new to objective-c, can someone tell me whats wrong with this?

1
  • Your curly braces aren't balanced. Commented Apr 12, 2011 at 5:24

2 Answers 2

4

You can't use the regular array-like subscripts with NSArrays (and NSMutableArrays). To add an item, you need to call the addObject method.. i.e. [charArray addObject:obj].

The other caveat is you can't add a bare char to an NSArray, it needs to be Objective-C type. You can use the NSNumber class to wrap it. So your code would then be:

[charArray addObject:[NSNumber numberWithChar:'0']];
[charArray addObject:[NSNumber numberWithChar:'1']];

But you can still use a regular C array and leave your code unmodified, which is probably a better solution in your case. i.e.

char charArray[MAX_SIZE];
Sign up to request clarification or add additional context in comments.

3 Comments

so is there no use of i++ here?
addObject appends a value to the end automatically.
consider these numbers num1 and num2 to be the binary version of the the outputs of shake events performed in two itouches. the ouput bits are the ones that are not similar between both these numbers. so bits in this array after being padded should be converted back to decimal number. Can this be done? Hope this made sense
3

You can't access an NSMutableArray using the [] syntax of C.

You need to use the -[NSMutableArray addObject:] or -[NSMutableArray insertObject:atIndex:] methods.

3 Comments

so how do i increment the value of i and place those values ?
@user531, You don't need i if you use addObject, as that will automatically add to the end for you.
an you take a look at the question that i put above in one of the comments and let me know if you can help me out with that?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.