我有一个自定义的NSManagedObject
有几个属性。I alloc
和init
两个实例的对象:compare1
&compare2
。然后我做一个NSFetchRequest
来获得两个自定义对象,并在NSArray
结构中填充它们的属性,以便稍后在UITableView
中显示它们。
我的问题是,UITableView
崩溃。我在代码中做了一些研究,发现有时数组没有完整的计数。当我玩我的滑块和文本字段,它有时工作,但它是(至少对我来说)不可复制。
提前感谢!
编辑:问题现在很清楚了。完成object description
。我通过NSArray * array = [[NSArray alloc] initWithObjects: object.value1, ..., nil];
将其转移到NSArray
。现在奇怪的是:[array count]
给了我一个错误的号码?数组中只有!= 0
的值。为什么如此?谢谢你!
以下是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
数组
中只有!= 0的值
不能将nil
值直接添加到数组中。(nil
通常为零。)如果您需要将"空"值添加到数组中,则需要检查并插入NSNull
值。例如:
NSArray * valuesS1 = [[NSArray alloc] initWithObjects:val1 ? val1 : [NSNull null], val2 ? val2 : [NSNull null], nil];
编辑
?:
相当于:
if (val1) {
return val1;
}
else {
return [NSNull null];
}
如果你想用@"0"
代替NSNull
,你可以;它可以是任何你喜欢的,只要它是一个对象,而不是nil
。