簡體   English   中英

如何遍歷自定義對象的NSArray

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

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

這意味着它是一系列假日對象。

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

看來您正在使用帶有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"]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM