2016-12-13 4 views
1

私はクラスRLMArrayにオブジェクトを追加しますか?

//Ingredient.h 
@interface Ingredient : RLMObject 

@property (nonatomic, copy, readwrite) NSString *name; 

@end 

RLM_ARRAY_TYPE(Ingredient) 

//Recipe.h 
@interface Recipe : RLMObject 

@property (nonatomic, strong, readwrite) RLMArray<Ingredients *>< Ingredients> *ingredients; 

@end 

を持っているが、それは、複数の成分を追加することが正しいですか?

Ingredient *tomato = [[Ingredient alloc] init]; 
tomato.name = @"tomato"; 
Ingredient *onion = [[Ingredient alloc] init]; 
onion = @"onion"; 

Recipe *recipe = [[Recipe alloc] init]; 

[realm beginWriteTransaction] 
[recipe.ingredients add:tomato]; 
[recipe.ingredients add:onion]; 
[realm.commitWriteTransaction]; 

その他の方法はありますか?

答えて

1

閉じる! RLMArrayは、NSMutableArrayのメソッド命名規則に従います。

だから、正しいメソッド名は

[realm beginWriteTransaction] 
[recipe.ingredients addObject:tomato]; 
[recipe.ingredients addObject:onion]; 
[realm.commitWriteTransaction]; 

で使用できるメソッドの完全なリストについては、RLMArray's documentationをチェックしてください。たとえば、オブジェクトを追加するためのより合理的な方法は、次のようになります。

+0

ありがとう!そして、私は 'トマト'が自動的に 'レルム'に追加されると思いますか? – Colas

+0

オブジェクトが既に 'RLMRealm'に属していない場合、' addObject'を呼び出すと追加されます。 – TiM

関連する問題