0

I'm creating an array like so:

NSString *str = [NSString stringWithString:@"testString"];
int id1 = 4;
NSArray *data = [NSArray arrayWithObjects:str, id1, @"TEST TEST TEST", nil];

But at runtime its coming up with "EXC_BAD_ACCESS" but theres no variable that isn't being declared :S

3 Answers 3

6

NSArray can only hold objects, and id1 is not an object. Use NSNumber to wrap it in an object that you can store in your array:

NSNumber *id1Obj = [NSNumber numberWithInt:id1];
Sign up to request clarification or add additional context in comments.

Comments

5

id1 is not an NSObject, it's a basic type. You can only add NSObject-derived objects to an NSArray.

The EXC_BAD_ACCESS is probably because, under the covers, it's trying to access an object stored at wherever your int is pointing to. In other words, it's using your int as a pointer to an NSObject and failing horribly when it does so.

Comments

4

Make id1 a NSNumber.

NSNumber *number = [NSNumber numberWithInt:id1];

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.