-1

我有 30 个房间,每个房间应该有 5 个相同的 RoomAttributes。

我在 Room 和 RoomAttributes 之间有多对多的关系。

我的解决方案是,创建 30 * 5 = 150 个 RoomAttributes 并为每个房间制作 NSSet 的 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;

我怎样才能让这更容易?

4

1 回答 1

2

**已编辑

啊,我明白了 - 对不起,我误解了原来的问题 - 编辑使它更容易。

为此,我将创建三个辅助方法

-(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
}
于 2012-05-03T08:50:31.450 回答