4

I need to store some integer values as the contents of an array. But when i try to do so, it throws a warning, passing argument 1 of 'addObject' makes pointer from integer without a cast. And obviously the value is not stored in the array. Here's thecode.

NSUInteger i;
for (i=0;i<5;i++){
 [array addObject:i];}

1 Answer 1

10

NSArray-s cannot store non-id objects. You have to box it into an NSNumber:

NSUInteger i;
for (i=0;i<5;i++) {
   [array addObject:[NSNumber numberWithUnsignedInteger:i]];
}

or use a CFArray with custom callbacks (but you sacrifice readability for performance), or use std::vector<NSUInteger> (but you need to use Objective-C++).

Sign up to request clarification or add additional context in comments.

3 Comments

@Nithin: You can get an NSUInteger from an NSNumber with -unsignedIntegerValue.
pls give an example for that.
NSUInteger anInt = [[array objectAtIndex:0] unsignedIntegerValue];

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.