2011-03-23 8 views
0

-setPropertiesToFetchとNSDictionaryResultTypeに設定された結果タイプを使用して、いくつかの属性と1対1の関係しか取得しません。 返された関係の属性にアクセスする際に問題が発生しました。できるだけ早く私は、プロパティにアクセスしたいと、私は「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 

私はすでにチェックしました、トン帽子は、辞書に返された値が正しいです。偶数座標はCoordinateオブジェクトを指します。

大変助かります。

+0

送信されたセレクターはなんですか?完全な例外情報を提供すると役に立つでしょう。 – freespace

+0

@freespace lonとlatは座標に送信されます –

+0

には完全な例外の説明が追加されました –

答えて

4

私は問題を解決することができました。あなたが関係のために得るのは、entityWithIDと共に使用してエンティティ自体を得ることができるNSManagedObjectIDです。 更新されたコードは次のとおりです。

- (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; 

} 
関連する問題