從 Objective-C 中的核心數據元素獲取和設置值? (Getting and Setting values from Core Data elements in Objective-C?)


問題描述

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

(by KajimbaumanTechZen)

參考文件

  1. Getting and Setting values from Core Data elements in Objective‑C? (CC BY‑SA 3.0/4.0)

#cocoa #objective-c #core-data #getter #setter






相關問題

為什麼 NSWindow 或 NSView 實例處理它自己的鍵事件,而不是它的委託? (Why does an NSWindow or NSView instance handle its own key events, and not its delegate?)

通過 IKImage 視圖預覽圖像 (Previewg images via IKImage View)

Gaya Jendela Kustom di Kakao (Custom Window Style in Cocoa)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Эквівалент ключа NMenuItem не працуе, калі меню схавана (NSMenuItem Key Equivalent not working when menu is hidden)

透明窗口中的圓形 NSView (Rounded NSView in a Transparent Window)

單元格中不可見的 UITextField 已將 userInteractionEnabled 設置為 No (UITextField in cell that is not visible has userInteractionEnabled set to No)

如何為自定義 NSImageRep 子類實現 -draw (How to implement -draw for custom NSImageRep subclass)

從 Objective-C 中的核心數據元素獲取和設置值? (Getting and Setting values from Core Data elements in Objective-C?)

來自 URL 編碼問題的 NSArray (NSArray from URL encoding problem)

停靠欄下的窗口化opengl遊戲中的雙光標 (Double cursor in windowed opengl game under dock)

在特定的 NSTableview 單元格中對角線繪製線條 (Draw Lines Diagonally in a particular NSTableview cell)







留言討論