2012-05-03 4 views
-1

私は30の部屋があり、各部屋は5つの同じRoomAttributesを持つ必要があります。NSSetをCoreDataに簡単に保存するにはどうすればよいですか?

私はRoomとRoomAttributesの関係が多岐にわたります。

私の解決策は、30 * 5 = 150のRoomAttributesを作成し、すべてのルームに対してNSAのRoomAttributesを作成することでした。これは仕事の割り当てです。

Raum *raum = [NSEntityDescription insertNewObjectForEntityForName:@"Raum" inManagedObjectContext:context]; 

raum.raumName = @"Main"; 
raum.etage = @"2. Stock, Raum 1.203"; 
raum.beschreibung = @"Gut beleuchtet"; 
raum.raumpreis = [NSNumber numberWithDouble:210]; 
raum.raumname = @"Besprechungsraum"; 

私はRoomAttributesを作成する方法:私はルームクリート方法

Raumattribute *attribute =[NSEntityDescription insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context]; 
    attribute.attributname = @"Beamer"; 
    attribute.schalter = [NSNumber numberWithBool:NO]; 

    Raumattribute *attribute2 =[NSEntityDescription insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context]; 
    attribute2.attributname = @"Behindertengerecht"; 
    attribute2.schalter = [NSNumber numberWithBool:NO]; 

私はNSSetの作成方法:

NSSet *attributeFurRaum = [NSSet setWithObjects:attribute1, attribute2,nil]; 
raum.raumattribute = attributeFurRaum; 

は、どのように私はこれを簡単にすることができますか?私は元の質問を誤解し申し訳ありません - - 編集が容易になり

答えて

2

**は、私が見

ああを編集しました。そのために

私は3つのヘルパーメソッド

-(RaumAttribute*)roomAttributeWithName:(NSString *)name andSchalter:(BOOL)schalter 
{ 
    Raumattribute *att =[NSEntityDescription insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context]; 
    att.attributname = name; 
    att.schalter = schalter; 
    return att; 
} 

-(NSSet *)roomAttributes 
{ 
    NSArray *atts = [@"Beamer,Behindertengerecht" componentsSeparatedByString:@","]; 
    NSMutableSet *roomAttributes = [NSMutableSet set]; 
    for(NSString *name in atts) 
    { 
     [roomAttributes addObject:[self roomAttributeWithName:name andSchalter:NO]]; 
    } 
    return roomAttributes; 
} 

-(Raum *)raumFromDictionary:(NSDictionary *)details 
{ 
    Raum *raum = [NSEntityDescription insertNewObjectForEntityForName:@"Raum" inManagedObjectContext:context]; 
    raum.raumName = [details valueForKey:@"raumName"]; 
    raum.etage = [details valueForKey:@"etage"]; 
    raum.beschreibung = [details valueForKey:@"beschreibung"]; 
    raum.raumpreis = [details objectForKey:@"raumpreis"]; 
    raum.raumname = [details objectForKey:@"raumname"]; 
    return raum; 
} 

を作成し、その後、あなたはPLISTまたはJSONであなたの所定のオブジェクトデータを格納することができ - 辞書にそれを解析し、その後のようなものに行く:

NSArray *raumDictionaries = //code to get array of dictionaries from a plist or whatever source 
NSSet *raumAttributeSet = [self roomAttributes]; 
for(NSDictionary *raumDict in raumDictionaries) 
{ 
    Raum *raum = [self raumFromDictionary:raumDict]; 
    raum.raumattribute = raumAttributeSet; 
    //save context 
} 
+0

私は私の質問を編集しました – Bashud

+0

私の回答を更新しました –

+0

私のコメントを追加しました – Manav

関連する問題