开发者

Cocoa Objective C, getting EXC_BAD_ACCESS in creating an array?

开发者 https://www.devze.com 2023-02-23 04:45 出处:网络
I\'m creating an array like so: NSString *str = [NSString stringWithString:@\"testString\"]; int id1 = 4;

I'm creating an array like so:

NSString *str = [NSString stringWithString:@"testString"];
int id1 = 4;
NSArray *data = [NSArray arrayWithObjects:str, id1, @"TEST T开发者_Go百科EST TEST", nil];

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


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];


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.


Make id1 a NSNumber.

NSNumber *number = [NSNumber numberWithInt:id1];
0

精彩评论

暂无评论...
验证码 换一张
取 消