Reading a plist into core data - NSDictionary within NSDictionary

后端 未结 1 1270
无人及你
无人及你 2021-01-03 06:41

I have a plist of values I want to read into Core Data. I can read in the \"top level,\" but along with strings and numbers, each item also contains dictionaries. I am havin

相关标签:
1条回答
  • 2021-01-03 06:50

    You are not creating a new managed object for each Plant dictionary value. You are just trying to assign the dictionary itself to the relationship which, of course, will not work.

    You need to add something like:

    [gardenManObj setValue:[thisGardenDictionary objectForKey:@"rating"] forKey:@"rating"];
    
    NSDictionary *thisGardenPlantsDictionary = [thisGardenDictionary objectForKey:@"plants"];
    NSLog(@"thisGardenPlantsDictionary contains %i dictionaries.", [thisGardenPlantsDictionary count]);
    NSManagedObject *plantMO;
    for (Plant *eachPlant in thisGardenPlantsDictionary) {
      plantMO=[NSEntityDescription insertNewObjectForEntityForName:@"Plant" inManagedObjectContext:context];
      [plantMO setValue:[eachPlant valueForKey:@"someValue" forKey:@"someKey"]];
       //... repeat for all attributes
      [plantMO setValue:gardenManObj forKey:@"gardent"];
    }
    

    ... that will create the necessary Plant objects and set the relationship with the correct Garden object.

    0 讨论(0)
提交回复
热议问题