This is a sample of an array I would like to get value from it.
coursesArray = [[NSMutableArray alloc]init];
// AR111:
[coursesArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"MUR",@"courseType",
@"AR111",@"courseCode",
@"Arabic Communication Skills (I)",@"courseName",
@"3",@"creditHours",
@"",@"preRequisites",
@"63.000",@"coursePrice",
@"6.000",@"courseEPP",
@"This course aims at .",@"courseDetails",
nil]];
And this is my code to get the value to it.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
int i = 0;
for (i = 0; i < [coursesArray count] ; i++) {
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:[[coursesArray objectAtIndex: i ]forKey:@"courseName"] forKey:@"courseName"];
}
If any one can help me to correct the following part:
[newManagedObject setValue:[[coursesArray objectAtIndex: i ]forKey:@"courseName"] forKey:@"courseName"];
The values will be add in to core data forKey:@”courseName”.
This is a classic case of putting too much code on one line, I think. I don’t expect this actually compiles?
Let’s try and break it down into sections. I’ve taken the central bit of code out and replaced it with XX:
This bit looks fine.
setValue:forKeyis a valid method for a managed object.XX is:
It’s not clear if this is right or not, but it doesn’t look good – the method name doesn’t seem right.
YY is:
This is fine, and it returns a mutable dictionary. But dictionaries don’t implement a method called
forKey:. They do implement a method calledobjectForKey:, though, which is what you want.So, your corrected line should be:
If you’re writing a lot of code like this, custom objects with properties to hold data members suddenly make a lot more sense. The forKey: all over the place soon becomes unreadable.