Iphone 使用setPropertiesToFetch获取后访问关系属性时出现问题

Iphone 使用setPropertiesToFetch获取后访问关系属性时出现问题,iphone,core-data,Iphone,Core Data,我只是使用-setPropertiesToFetch和设置为NSDictionaryResultType的结果类型来获取实体的一些属性和1对1关系。 现在我在访问返回关系的属性时遇到问题。只要我想访问一个属性,我就会得到一个NSInvalidArgumentException',原因:无法识别的选择器被发送到实例 以下是完全例外: 2011-03-23 11:02:10.435 ThurboApp[32996:207] -[_NSObjectID_48_0 lon]: unrecognized

我只是使用-setPropertiesToFetch和设置为NSDictionaryResultType的结果类型来获取实体的一些属性和1对1关系。 现在我在访问返回关系的属性时遇到问题。只要我想访问一个属性,我就会得到一个NSInvalidArgumentException',原因:无法识别的选择器被发送到实例

以下是完全例外:

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
我已经检查了字典中返回的值是否正确。偶数坐标指向坐标对象


非常感谢您的帮助。

好的,我设法解决了这个问题。关系得到的是一个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;

}

好的,我设法解决了这个问题。关系得到的是一个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;

}

那么发送的选择器是什么?提供完整的异常信息会很有帮助。@freespace lon和lat被发送到coordinates添加完整的异常描述,就像它是一个接收选择器的通用对象,而不是
Coordinate
或甚至是
NSManagedObject
子类。在分配给coords后添加此项:if([coords isKindOfClass:[Coordinates class]])NSLog(@“是坐标实例”);看看它是否打印出来。它没有,所以我怎么做似乎有错误,但我不知道它是什么…那么发送的选择器是什么?提供完整的异常信息会很有帮助。@freespace lon和lat被发送到coordinates添加完整的异常描述,就像它是一个接收选择器的通用对象,而不是
Coordinate
或甚至是
NSManagedObject
子类。在分配给coords后添加此项:if([coords isKindOfClass:[Coordinates class]])NSLog(@“是坐标实例”);看看有没有印出来。没有,所以我怎么做似乎有点错误,但我不知道是什么。。。