7

This must be quite basic, but I was wondering how to add an integer to an array?

I know I can add strings like this:

NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:@"0"];
[trArray addObject:@"1"];
[trArray addObject:@"2"];
[trArray addObject:@"3"];

But I guess I can't simply add integers like so:

NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:0];
[trArray addObject:1];
[trArray addObject:2];
[trArray addObject:3];

At least the compiler isn't happy with that and tells me that I'm doing a cast without having told it so.

Any explanations would be very much appreciated.

3 Answers 3

29

Yes that's right. The compiler won't accept your code like this. The difference is the following:

If you write @"a String", it's the same as if you created a string and autoreleased it. So you create an object by using @"a String".

But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.

NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];

To retrive the integer again, do it like this

NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];

I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.

EDIT:

It is now also possible to write the following

[yourArray addObject:@3];

which is a shortcut to create a NSNumber. The same syntax is available for arrays

@[@1, @2];

will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.

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

6 Comments

Thanks, I think I understand this now. Your explanation is very good!
Thank you. The answer of mservidio is absolutly correct. But I always like to have an explanation. ;-)
Just a follow-up question: Is 'number' stored in my array an integer (int) or an NSInteger? Or doesn't this really matter?
As far as I know the difference is only a difference in the size which is used to store that value. I normally use int. The only reason for me: it's less to write. ;-) Perhaps there's someone around here who can explain the differnce. I can't.
This is wrong, string literals aren't autoreleased. They have static storage duration and the highest possible reference count (which indicates that their refcount can't even be decreased by release).
|
7

You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];

NSMutableArray *trArray = [[NSMutableArray alloc] init];     
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];

[trArray addObject: yourNumber];

2 Comments

mservidio is correct. I believe the way to think of this is because @"2" is a NSString object. However 2 by itself is a value, not an object.
to expand, you basically create an instance of the NSNumber object that is a object version of the primitive int or whatever and then adds this object to the array. Also, to get the primitive value back out of the array, use [[myArray objectAtIndex:0] intValue];
4

You can also use this if you want to use strings:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:@"%d",1]];
[[array objectAtIndex:0] intValue];

Comments

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.