1

2つのエンティティを持つ単純なアプリケーションがあります。

人:

Attributes:
    name
Relationships:
    nativeLanguage: (<<-> Language.natives)
    nonNativeLanguage: (<<-> Language.nonNatives)

言語:

Attributes:
    name
Relationships:
    natives: (<->> Person.nativeLanguage)
    nonNatives: (<->> Person.nonNativeLanguage)

人々の編集画面には、その人々のためにリストされた非母国語をリストするためのテーブル表示セットと、別の言語を選択するためのドロップボックスと新しい言語をリストに追加するためのボタンがあります。

私の目標は、次のような関数を作成することです。

- (IBAction)addNonNativeLanguage:(id)sender {
    // tack the language to the end of the list on a many-many relationship
    PersonEntry.nonNativeLanaguages = PersonEntry.nonNativeLanguages + sender;
}

アクションをボタンに添付します。トリックは、通常の変数を割り当てて変更する方法は知っていますが、InterfaceBuilderの外部でコアデータのコンテンツを変更する方法がわかりません。Objective-Cでそれを行うにはどうすればよいですか?

4

2 に答える 2

2

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.

于 2009-12-06T20:30:59.223 に答える
1

まず、必要なmanagedObjectインスタンス/レコードを見つける必要があります。手っ取り早い方法は、personオブジェクトを表示しているインターフェイス要素に、どのオブジェクトを持っているかを尋ねることです。

使用するオブジェクトを取得したらmutableSetValueForKey:、リレーションシップの名前を使用して呼び出します。これにより、関連するすべての言語の変更可能なセットが提供されます。次に、適切な言語オブジェクトをフェッチし、標準のNSMutableSetメソッドを使用してセットに追加します。

于 2009-12-06T16:10:27.713 に答える