我在setPropertyToFetch
时收到错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray initWithObjects:count:]: attempt to insert nil object at objects[0]'
代码:
NSString *str;
str = [NSString stringWithFormat:@"Event%d", variable];
NSString *value = [NSString stringWithFormat:@"values%d", vari];
TermometrAppDelegate *app;
app = (TermometrAppDelegate *)[UIApplication sharedApplication].delegate;
NSFetchRequest *fetchRequests = [[NSFetchRequest alloc] init];
NSEntityDescription *entit = [NSEntityDescription entityForName:str inManagedObjectContext:app.managedObjectContext];
[fetchRequests setEntity:entit] ;
[fetchRequests setReturnsDistinctResults:YES];
NSDictionary *entityProperties = [entit propertiesByName];
[fetchRequests setPropertiesToFetch :[NSArray arrayWithObject:[entityProperties objectForKey:value]]];
NSError *error;
fetchedObject = [app.managedObjectContext executeFetchRequest:fetchRequests error:&error];
NSManagedObject *fetched ;
printf("\n%d", [fetchedObject count]);
if ([fetchedObject count]==0)
{ for (int h=0;h<31;h++){ NSManagedObject *Event = [NSEntityDescription insertNewObjectForEntityForName:str inManagedObjectContext:app.managedObjectContext];
[Event setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:value]; [app.managedObjectContext save:nil] ;}}
else {fetched=[fetchedObject objectAtIndex:0];
if([[fetched valueForKey:value] intValue]==0) {
for (int h=0;h<31;h++){ [[fetchedObject objectAtIndex:h] setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:value];}
}
else{ printf("\n%d", buf4[v]);
for (int h=0;h<31;h++){ fetched=[fetchedObject objectAtIndex:h];
int plus=[[fetched valueForKey:value] intValue];
plus=(plus+buf4[v+h])/2;
[[fetchedObject objectAtIndex:h] setValue:[NSNumber numberWithInt:plus] forKey:value];}
}
}
[fetchRequests release];
[fetchedObject release];
**我忘了创建Enitity7 ... Entity64歇斯底里 - 在实体中获取属性的问题为期2周。
我认为你需要编写语言,每个单词 - 一个普通的文字 - 这意味着命令汇编程序。无论如何,你需要努力将编程语言作为纯数学和口语的语言。该语言与口语一样自由,客观编程将我们置于监狱**
答案 0 :(得分:0)
最简单的解释是,value
字符串的构造不正确,因此[entityProperties objectForKey:value]
会返回nil
值。
使用构造的实体和属性名称是一种危险的做法。这种结构非常非常容易出错并且导致细微且难以找到错误。为实体和属性提供唯一且有意义的名称并使用它们。
答案 1 :(得分:0)
抛出异常是因为你试图将nil放入NSArray中。我看到你创建一个数组的唯一一次是:
[NSArray arrayWithObject:[entityProperties objectForKey:value]]
要检查的重点是:
[entityProperties objectForKey:value]
看看那是不是。如果那是零则然后向后工作以找出原因。