問題描述
從 Objective‑C 中的核心數據元素獲取和設置值? (Getting and Setting values from Core Data elements in Objective‑C?)
I've got a simple application with two entities:
Person:
Attributes:
name
Relationships:
nativeLanguage: (<<‑> Language.natives)
nonNativeLanguage: (<<‑> Language.nonNatives)
Language:
Attributes:
name
Relationships:
natives: (<‑>> Person.nativeLanguage)
nonNatives: (<‑>> Person.nonNativeLanguage)
On the edit screen for people, I've got a table display set to list the non‑native languages listed for the person, along with a drop box for them to choose another language and a button to add the new language to the list.
My goal is to create a function as follows:
‑ (IBAction)addNonNativeLanguage:(id)sender {
// tack the language to the end of the list on a many‑many relationship
PersonEntry.nonNativeLanaguages = PersonEntry.nonNativeLanguages + sender;
}
and attach the action to the button. Trick is, while I know how to assign and modify regular variables, I have no idea how to modify the contents of Core Data outside of Interface Builder. How can I do it in Objective‑C?
‑‑‑‑‑
參考解法
方法 1:
In all my Core Data model entity classes, I have the following code for to‑many relationship manipulation:
‑ (void)addNatives:(NSSet*)value_ {
[self willChangeValueForKey:@"natives" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value_];
[[self primitiveValueForKey:@"natives"] unionSet:value_];
[self didChangeValueForKey:@"natives" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value_];
}
‑(void)removeNatives:(NSSet*)value_ {
[self willChangeValueForKey:@"natives" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value_];
[[self primitiveValueForKey:@"natives"] minusSet:value_];
[self didChangeValueForKey:@"natives" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value_];
}
‑ (void)addNativesObject:(Person*)value_ {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value_ count:1];
[self willChangeValueForKey:@"natives" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"natives"] addObject:value_];
[self didChangeValueForKey:@"natives" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
‑ (void)removeNativesObject:(Person*)value_ {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value_ count:1];
[self willChangeValueForKey:@"natives" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"natives"] removeObject:value_];
[self didChangeValueForKey:@"natives" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
}
‑ (NSMutableSet*)nativesSet {
[self willAccessValueForKey:@"natives"];
NSMutableSet *result = [self mutableSetValueForKey:@"natives"];
[self didAccessValueForKey:@"natives"];
return result;
}
This template code is from Wolf Rentzsch's very excellent mogenerator (which automatically generates it appropriately upon every save of the data model).
Also, note that the Core Data backend for to‑many relationships is unordered. If you want to keep an order to your data, you have to do it yourself.
方法 2:
You will first have to find the managedObject instance/record you want. The quick and dirty way is to ask the interface element that is displaying the person object which object it has.
Once you have the object you use call mutableSetValueForKey:
with the name of the relationship. This gives you a mutable set of all the related languges. You then fetch the proper language object and add it to the set using the standard NSMutableSet methods.