有一個CoreData中的一對多關系。現在想從coredata實體中創建一個對象,然後設置為NSSet。
我想實例化對象沒有錯,但是再設置NSSet時就不對。
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSSet *selectedCategories = [NSSet set];
for (NSString *categoryName in self.categories /*NSMutableArray propterty*/) {
Category *category = [NSEntityDescription
insertNewObjectForEntityForName:@"Category" inManagedObjectContext:context];
category.name = categoryName;
[selectedCategories setByAddingObject:category];
}
[self.delegate addCategoryViewControllerDidSelectCategory:
self didSelectCategories:selectedCategories];
這行:
[selectedCategories setByAddingObject:category];
創建了一個沒有指定變量的新設置,試試用NSMutableSet
NSMutableSet *selectedCategories = [NSMutableSet set];
for (NSString *categoryName in self.categories /*NSMutableArray propterty*/) {
Category *category = [NSEntityDescription
insertNewObjectForEntityForName:@"Category" inManagedObjectContext:context];
category.name = categoryName;
[selectedCategories addObject:category];
}