2013-01-03 38 views
1

我创建一个NSArray从CoreData取像这样:我如何通过自定义的一个NSArray循环对象

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

在的tableview我用这个代码,让我的自定义对象:

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

但是,我现在在另一个viewcontroller,试图绘制一个mapkit上的数据,所以在绘图方法我原来这样做是因为我从一个plist文件中获取数组。但现在我的阵列是定制的假日的对象,因此这不工作了:

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++

回答

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"] 
}