0

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".

2
  • is this correct [newManagedObject setValue:[[coursesArray objectAtIndex:i ]objectForKey:@"courseName"] forKey:@"courseName"]; Commented Aug 6, 2012 at 19:42
  • Try and rephrase what you have written so that it becomes a clear question. Something like "How can I read ... and set the it on ... ?" Commented Aug 6, 2012 at 19:58

1 Answer 1

3
[newManagedObject setValue:[[coursesArray objectAtIndex: i ]forKey:@"courseName"] 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:

[newManagedObject setValue:XX forKey:@"courseName"];

This bit looks fine. setValue:forKey is a valid method for a managed object.

XX is:

[YY forKey:@"courseName"]

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:

[coursesArray objectAtIndex:i]

This is fine, and it returns a mutable dictionary. But dictionaries don't implement a method called forKey:. They do implement a method called objectForKey:, though, which is what you want.

So, your corrected line should be:

[newManagedObject setValue:[[coursesArray objectAtIndex:i] objectForKey:@"courseName"] forKey:@"courseName"];

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was about to break it down and saw this: "This is a classic case of putting too much code on one line" +1!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.