2011-03-23 64 views
0

我只使用-setPropertiesToFetch獲取某個實體的一對一關係,並將結果類型設置爲NSDictionaryResultType。 現在,我在訪問返回關係的屬性時遇到問題。當我想訪問的屬性我得到一個NSInvalidArgumentException」的,理由是:無法識別的選擇發送到實例使用setPropertiesToFetch獲取關係後訪問屬性的問題

以下是完整的異常:

2011-03-23 11:02:10.435 ThurboApp[32996:207] -[_NSObjectID_48_0 lon]: unrecognized selector sent to instance 0x5a3f490 
2011-03-23 11:02:10.441 ThurboApp[32996:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_48_0 lon]: unrecognized selector sent to instance 0x5a3f490' 

下面是相應的源代碼:

- (void)fetchAllPois 
{ 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"POI" inManagedObjectContext:self.managedObjectContext]; 
    request.entity = entity; 
    [request setResultType:NSDictionaryResultType]; 
    [request setPropertiesToFetch:[NSArray arrayWithObjects:@"poiId",@"categoryId",@"poiTitle",@"coordinates", nil]]; 
    request.sortDescriptors = nil; 
    request.predicate = nil; 

    NSError *error = nil; 
    self.fetchResult = [self.managedObjectContext executeFetchRequest:request error:&error]; 

} 


- (MapPoint *)createMapPointFromDictionary:(NSDictionary *)dict 
{ 
    NSString *poiId = [dict objectForKey:@"poiId"]; 
    NSString *title = [dict objectForKey:@"poiTitle"]; 
    Coordinates *coords = (Coordinates *)[dict objectForKey:@"coordinates"]; 
    NSNumber *category = [dict objectForKey:@"categoryId"]; 
    CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([coords.lat doubleValue], [coords.lon doubleValue]); //here raises the exception 
    MapPoint *p = [[[MapPoint alloc] initWithPoiId:poiId title:title category:[category intValue] coordinates:coordinates] autorelease]; 
    return p; 

} 

接口座標:

@interface Coordinates : NSManagedObject 
{ 
} 

@property (nonatomic, retain) NSNumber * lat; 
@property (nonatomic, retain) NSNumber * alt; 
@property (nonatomic, retain) NSNumber * lon; 
@property (nonatomic, retain) NSManagedObject * poi; 

@end 

我已經檢查,T在字典中返回的值是正確的。偶座標指向座標對象。

非常感謝幫助。

+0

那麼發送的選擇器是什麼?提供完整的例外信息將會有所幫助。 – freespace 2011-03-23 08:33:55

+0

@freespace lon和lat被髮送到座標 – 2011-03-23 10:01:09

+0

添加了完整的異常描述 – 2011-03-23 10:04:21

回答

4

好吧,我設法解決這個問題。你得到的關係是NSManagedObjectID,你可以使用objectWithID來獲取實體本身。 這裏是更新的代碼:

- (MapPoint *)createMapPointFromDictionary:(NSDictionary *)dict 
{ 
    NSString *poiId = [dict objectForKey:@"poiId"]; 
    NSString *title = [dict objectForKey:@"poiTitle"]; 
    NSManagedObjectID *coordsID = (NSManagedObjectID *)[dict objectForKey:@"coordinates"]; //get the objectid 
    NSNumber *category = [dict objectForKey:@"categoryId"]; 
    Coordinates *coords = (Coordinates *)[self.managedObjectContext objectWithID:coordsID]; //get the actual managedobject 
    CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([coords.lat doubleValue], [coords.lon doubleValue]); 
    MapPoint *p = [[[MapPoint alloc] initWithPoiId:poiId title:title category:category coordinates:coordinates] autorelease]; 
    return p; 

}