1

我从 CoreData 获取中创建了一个 NSArray,如下所示:

self.farSiman = [self.managedObjectContext executeFetchRequest:request error:&error];

在表格视图中,我使用此代码来获取我的自定义对象:

Holiday *holiday = [self.dates objectAtIndex:indexPath.row];
cell.nameLabel.text = holiday.name;

但是我现在在另一个视图控制器中,试图在 mapkit 上绘制数据,所以在绘图方法中我最初这样做是因为我从 plist 文件中获取了一个数组。但是现在我的数组是自定义的 Holiday 对象,所以这不再起作用了:

NSLog(@"dictionary is %@", self.farSiman);

for (NSDictionary * dict in self.farSiman) {


    NSNumber * latitude = [dict objectForKey:@"latitude"];
    NSNumber * longitude = [dict objectForKey:@"longitude"];
    NSString * storeDescription = [dict objectForKey:@"name"];
    NSString * address = [dict objectForKey:@"address"];
    NSLog(@"logging location %@", storeDescription);
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;
    MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate];
    [_mapView addAnnotation:annotation];

}

我的字典日志打印出这个:

dictionary is (
"<Holiday: 0x838bc80> (entity: Holiday; id: 0x838ca60 <x-coredata://E41B0CCD-2F03-4C4F-B054-18537096771C/Holiday/p1> ; data: <fault>)",
"<Holiday: 0x838e330> (entity: Holiday; id: 0x838ca70 <x-coredata://E41B0CCD-2F03-4C4F-B054-18537096771C/Holiday/p2> ; data: <fault>)"

这意味着它是一系列假日对象。

由于我使用枚举而不是传统的,如何在我的 for 循环中获取每个对象for i = 0; i<count; i++

4

1 回答 1

5

看起来您正在使用带有 CoreData 的自定义对象,因此它将返回您的类的数组。

这是否有效:

for (Holiday *holiday in self.farSiman) {
    // your code here
    // [holiday foo]
}

如果 CoreData 没有使用您的自定义对象,它将返回一个 NSManagedObject 数组,在这种情况下使用这个:

for (NSManagedObject *holiday in self.farSiman) {
    // your code here
    //[holiday valueForKey:@"foo"]
}
于 2013-01-04T03:16:19.037 回答