简体   繁体   English

如何遍历自定义对象的NSArray

[英]How do I loop through an NSArray of Custom Objects

I created an NSArray from a CoreData fetch like so: 我从CoreData提取创建了NSArray,如下所示:

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

In a tableview I used this code to get my custom objects: 在表格视图中,我使用以下代码来获取我的自定义对象:

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

But Im now in another viewcontroller, trying to plot the data on a mapkit, so in the plotting method i originally did this because i was getting an array from a plist file. 但是我现在在另一个视图控制器中,试图在mapkit上绘制数据,所以在绘制方法中,我最初是这样做的,因为我是从plist文件中获取数组的。 But now my array is of custom Holiday objects so this doesnt work anymore: 但是现在我的数组是自定义的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];

}

My dictionary log prints out this: 我的字典日志显示​​如下:

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>)"

Which means its an array of holiday objects. 这意味着它是一系列假日对象。

How do I get each object in my for loop since Im using enumeration instead of a traditional for i = 0; i<count; i++ 因为Im使用枚举而不是传统for i = 0; i<count; i++所以如何在for循环中获取每个对象for i = 0; i<count; i++ for i = 0; i<count; i++ for i = 0; i<count; i++ ? for i = 0; i<count; i++

It looks like you are using a custom object with CoreData, so it will be returning an array of your class. 看来您正在使用带有CoreData的自定义对象,因此它将返回您的类的数组。

Does this work: 这个工作:

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

If CoreData is not using your custom object, it will return an array of NSManagedObject, in which case use this: 如果CoreData没有使用您的自定义对象,它将返回一个NSManagedObject数组,在这种情况下,请使用此数组:

for (NSManagedObject *holiday in self.farSiman) {
    // your code here
    //[holiday valueForKey:@"foo"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM