views:

37

answers:

1

Can someone tell me why my application crashes here ?

and why it does not crash when i replace the YES objects with NSString values ?

all i want to do is to store boolean data into array and to modify these data later, can someone please tell me how to do this ?

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray* arr = [[NSMutableArray alloc] initWithObjects:YES, YES, YES, YES, nil];
    NSLog([arr objectAtIndex:1]);
}
+2  A: 

YES and NO are BOOLs, which is not an Objective-C class. Foundation containers can only store Objective-C objects.

You need to wrap them in an NSNumber, like:

NSNumber* yesObj = [NSNumber numberWithBool:YES];
NSMutableArray* arr = [[NSMutableArray alloc] initWithObjects:
                                               yesObj, yesObj, yesObj, yesObj, nil];
NSLog(@"%d", [[arr objectAtIndex:1] boolValue]);

The reason why it accepts NSString is because an NSString is a kind of Objective-C class.

KennyTM
To get the original `BOOL` value, you can use `BOOL b = [[array objectAtIndex:i] boolValue]`.
dreamlax
Thank you for the fast reply, i wish objective-c had the feature of auto boxing / unboxing as it is in Java, anyway objective-c is really a painfull language :S
anasnakawa
@anasnakawa: Objective-C is quite a thin layer over plain C, which is where most of the pain will come from.
dreamlax
@anasnakawa: that's what I thought at first, but in many ways Objective-C is closer to the spirit of real object oriented programming than Java is. Once you get used to the quirks, you may find it to be better.
JeremyP