How to work with booleans in NSMutableArrays?

Can someone tell me why my app crashes here?

and why doesn't it crash when I replace YES objects with NSString values?

all i want to do is to store the boolean data in an array and change this 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]); } 
+4
source share
1 answer

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

You need to wrap them in NSNumber, for example:

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

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

+14
source

All Articles