2
NSMutableArray *experienceValues;
experienceValues = [NSMutableArray alloc] initWithObjects:0,83,174,276,nil];

NSLog(@"%@", [experienceValues objectAtIndex:3]);

Why does this always throw -[__NSArrayM objectAtIndex:]: index 3 beyond bounds for empty array when it is clearly allocated and initialised in the line just before?

2
  • 1
    You're question has already been well answered, but do you not receive any warnings or errors at line 2? Commented Aug 29, 2013 at 11:16
  • First off why not make lines 1 and 2 just one line? Second your missing a [ on line 2 just before [NSMutableArray all..., and finally you need @ before each integer. Commented Aug 29, 2013 at 12:03

6 Answers 6

3

You need to wrap integer values like @0, @83, @174, ..., as primitive integers are no objects.

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

1 Comment

Perfect, exactly what i was looking for.
3

The NSMutableArray class as most of the collection classes in cocoa, accepts only objects. So if you want to put numbers, you can't put primitive type, but only instances of NSNumber class, thus:

[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0]...etc

Or with literals:

[[NSMutableArray alloc] initWithObjects:@0,@83,@174,@276.. etc

3 Comments

Brilliant, thank you, but i cant accept any of the answers for 8 more minutes
No big deal, you are welcome. I'd like also to point out that when you get a value back as you'd like, you get an NSnumber object, so you can't do "math operation" on it. So [[experienceValues objectAtIndex:3]intValue] will return the correct primitive type.
Remark: @0, @83 are "NSNumber literals". "Subscripting" is e.g. array[0] or dict[@"key"].
2

The array is empty because the arguments are nil terminated and 0 is interpreted as nil.

The array should only contain objects (as opposed to primitives like ints). In you case you should create NSNumbers for all the numbers. You can do that with the number literal syntax @2.0.

Comments

1

Objective-C collection classes can only store objects; not primitives like int.

You need to wrap them in NSNumber objects.

If these are literal values then the answer provided by @Kirsteins demonstrates the syntax.

Comments

1

Try this

NSMutableArray *experienceValues = [[NSMutableArray alloc] initWithObjects:@0,@83,@174,@276,......., nil];

NSLog(@"%d", [experienceValues objectAtIndex:3]);

As initWithObjects: accepts only object you need to put only objects in it

2 Comments

I think @0 (NSNumber) would be better than @"0" (NSString)
Thanks for advice and yes it is sure better :)
1

You're trying to add integer value to NSArray.

NSMutableArray *experienceValues = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:42],nil];

then convert it back to integer like,

NSLog(@"%@", [experienceValues objectAtIndex:0]);

2 Comments

Here you are adding strings, rather than NSNumber literals
@c.cam108..yes,thats not the ideal way to do..i agree with u and do apolozige for the old method of adding strings and then converting it to integer...so edited the answer..

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.