Question

Why I am getting a MSRangeException with this code:

NSArray *patientenVornamen = [NSArray arrayWithObjects:@"Michael", @"Thomas", @"Martin", nil];
NSArray *patientenNachnamen = [NSArray arrayWithObjects:@"Miller", @"Townsend", @"Mullins", nil];
NSArray *patientenWeiblich = [NSArray arrayWithObjects:NO, NO, NO , nil];
NSArray *patientenGeburtsdatum = [NSArray arrayWithObjects:[NSDate date], [NSDate date], [NSDate date], nil];
for (int i = 0; i < 3; i++) {
    Patient *patient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:_coreDataHelper.context];
    patient.vorname = [patientenVornamen objectAtIndex:i];
    patient.nachname = [patientenNachnamen objectAtIndex:i];
    patient.geburtsdatum = [patientenGeburtsdatum objectAtIndex:i];
    patient.weiblich = [patientenWeiblich objectAtIndex:i];
}
Was it helpful?

Solution

NSArray *patientenWeiblich = [NSArray arrayWithObjects:NO, NO, NO , nil];

You can't put primitive types into NSArray. In your case compiler is silent just because NO is 0 which is effectively nil. So, you get an empty NSArray.

You should wrap them with NSNumber first:

NSArray *patientenWeiblich = [NSArray arrayWithObjects:@(NO), @(NO), @(NO) , nil];

And afterwards get the value with:

[[patientenWeiblich objectAtIndex:i] boolValue];

OTHER TIPS

You cannot put a bool value as is. Instead use @(NO). That should work.

NO is not an object but you need to store objects inside of an array. So use [NSNull null] or @(NO) instead of NO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top