开发者

Error while fetch in core data

I get an error with setPropertyToFetch.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray initWithObjects:count:]: attempt to insert nil object at objects[0]'

code:

 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++){ NS开发者_StackOverflowManagedObject *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];

** I forgot Create Enitity7 ... Entity64 hysterics - 2 week on problem to fetch attributes in entity.

I think you need to do programming language where each word - a common literary word - which means the command assembler. In any case, you need to strive for the programming language as the language of pure mathematics and the spoken language. That the language was as free as a spoken language, objectiv programming puts us in jail **


The simplest explanation is that the construction of your value string is incorrect and that therefore the [entityProperties objectForKey:value] returns a nil value.

Using constructed entity and property names is a dangerous practice. It is very, very easy for the construction to go wrong and cause subtle and hard to locate bugs. Give entities and properties unique and meaningful names and use them.


The exception is being thrown because you are trying to put nil into an NSArray. The only time I see you creating an array is:

[NSArray arrayWithObject:[entityProperties objectForKey:value]]

The big thing to check would be:

[entityProperties objectForKey:value]

and see if that was nil. If that's nil then work backward to find out why.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜