Hi i am saving values coming from a database in a MultableArray and then in CoreData:
NSMultableArray *mutary = [[NSMultableArray alloc] init];
NSManagedObjectContext *context = [app managedObjectContext];
for(int n=0; n<[AttributeArray count]; n++)
{
[mutary addObject:[[AttributeArray objectAtIndex:n] objectForKey:@"AttributName"]];
NSLog(@"%@", mutary);
}
attributeString = [mutary componentsJoinedByString:@","];
raume = [NSEntityDescription insertNewObjectForEntityForName:@"Raum" inManagedObjectContext:context];
raume.raumattribut = attributeString;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSLog output for the MultableArray is:
2012-06-20 17:21:00.047 book-app[31984:15803] (
A7OVERHEAD,
Beamer
)
So far its working correct. The two expected values from the database are now in the Array.
Now i am fetching these attributes from CoreData:
NSManagedObjectContext *context = [app managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Raum" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error = nil;
NSArray *events = [context executeFetchRequest:request error:&error];
for (Raum *att in events)
{
stringAttribute = [[events valueForKey:@"raumattribut"] componentsJoinedByString:@","];
NSLog(@"ATTRIBUTE: %@", stringAttribute);
}
}
So far so good. But if i look now on my NSLog output:
2012-06-20 17:21:00.055 book-app[31984:15803] ATTRIBUTE: <null>,A7OVERHEAD,Beamer
CoreData is returning and then the two values. Where is that coming from?
Can someone help?
Thanks in advance
EDIT: After some investigating and clarifying (see comments), here is my answer:
Calling valueForKey on an NSArray returns an array where each element is created by calling valueForKey on each member of the original array. This means your output indicates that you have 3 total objects returned by your fetch request, and the first one does not have it’s raumattribut attribute set.
PREVIOUS ANSWER:
You are calling valueForKey on an instance of NSArray (the query results). Perhaps you need to
get objectAtIndex:0first use the variable you are iterating the values on? valueForKey is likely returning nil.or similar.
EDIT:
At first I missed the for loop in the second part, I have edited the above code sample to use the attr, which