从Core Data获取NSSet并在Label中显示它

时间:2012-03-29 14:30:01

标签: ios xcode core-data

我有一套房间属性(NSString),我将它们存储在CoreData中。我如何获取它们以在UILabel中显示它?

我在viewDidLoad中试过这个:

    NSArray *array = [[currentRaum raumattribute] allObjects];

    [roomAttributLabel setText:[NSString stringWithFormat:@"%@",array]];

但它不起作用。它显示了一些核心数据代码Screenshot

这是我保存内容的方式:

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


raum2.raumName = @"Main";
raum2.ort = @"Aschaffenburg";
raum2.strasse = @"Bruchtannenstr. 11";
raum2.etage = @"2. Stock, Raum 1.203";
raum2.beschreibung = @"Gut beleuchtet";

UIImage *img2 = [UIImage imageNamed:@"Main.jpg"];
NSData *data2 = UIImagePNGRepresentation(img2);
raum2.foto = data2;


NSSet *attributeFurRaum2 = [NSSet setWithObjects:attribute4,attribute5,attribute6,nil];

raum2.raumattribute= attributeFurRaum2;

currentRaum声明:

 self.currentRaum = [self.fetchedResultsController objectAtIndexPath:indexPath];

我期待3个不同的NSStrings保存如下:

Raumattribute *attribute4 =[NSEntityDescription    insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context];
attribute4.attributname = @"Copyboard";


Raumattribute *attribute5 =[NSEntityDescription insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context];
attribute5.attributname = @"Flipchart";


Raumattribute *attribute6 =[NSEntityDescription insertNewObjectForEntityForName:@"Raumattribute" inManagedObjectContext:context];
attribute6.attributname = @"Internetmöglichkeit";

1 个答案:

答案 0 :(得分:0)

您将不得不遍历数组元素以将它们添加到您的字符串中。根据您希望字符串的显示方式,它可能类似于:

NSString *compositeString = [ [ NSString alloc ] init ];
for( RaumAttribute *attr in array )
    compositeString = [ NSString stringWithFormat:@"%@ %@", compositeString, RaumAttribute.attributeName ];
[roomAttributLabel setText:compositeString];

如果我有正确的字段名称。编辑答案时我看不出问题......