I have a custom NSManagedObject which has several properties. I alloc and init two instances of the object: compare1 & compare2. Then I do a NSFetchRequest to get two of these custom objects and fill their properties in an NSArray structure to display them in a UITableView later.
My problem is, that the UITableView crashes. I did some research in the code and found out, that sometimes the array does not have the full count. When I play with my sliders and text field it works sometimes, but it is (at least for me) not reproducable.
Thank you in advance!
EDIT: The problem is clear now. The object description is complete. I transfer it to an NSArray via NSArray * array = [[NSArray alloc] initWithObjects: object.value1, ..., nil];. Now the strange thing: [array count] gives me a wrong number? Only the values which are != 0 are in the array. Why so? Thank you!
Here is the code of the NSFetchRequest:
-(NSArray *)performFetch
{
if (__managedObjectContext == nil)
{
__managedObjectContext = [(MasterViewController *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSError *error = nil;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"FetchedObjects Count: %i", [fetchedObjects count]); //always right
compare1 = [fetchedObjects objectAtIndex:0]; //compare is a custom NSObject
compare2 = [fetchedObjects objectAtIndex:1]; //with several properties (.1 to .17)
NSLog(@"%@",[compare1 description]); //is complete
NSLog(@"%@",[compare2 description]); //is complete
NSArray * valuesS1 = [[NSArray alloc] initWithObjects:compare1.1,__andsoon__compare1.14, nil];
NSArray * valuesB1 = [[NSArray alloc] initWithObjects:compare1.14__andsoon__compare1.17, nil];
NSArray * valuesS2 = [[NSArray alloc] initWithObjects:compare2.1,__andsoon__compare2.14, nil];
NSArray * valuesB2 = [[NSArray alloc] initWithObjects:compare2.14__andsoon__compare2.17, nil];
NSMutableArray * valuesArray1 = [[NSMutableArray alloc] initWithObjects:valuesS1, valuesB1, nil];
NSMutableArray * valuesArray2 = [[NSMutableArray alloc] initWithObjects:valuesS2, valuesB2, nil];
compareArray = [[NSMutableArray alloc] initWithObjects:valuesArray1, valuesArray2, nil];
NSLog(@"Array1 count: %i",[values1 count]); //sometimes (I don't know why)
NSLog(@"Array2 count: %i",[values2 count]); //[values1 count] != [values2 count]
return compareArray; //sometimes returns an array with too less objects so my UITableView crashes
You can’t add
nilvalues directly to an array. (nilis usually zero.) If you need to add “empty” values to an array you need to check and insert anNSNullvalue instead. For example:Edit
The
?:is equivalent to:If you want to use
@"0"instead ofNSNullyou can; it can be anything you like as long as it’s an object and is notnil.