NSManagedObject getter/setter 无法识别的选择器错误
我收到以下错误。我不明白为什么,因为 latitude
和 longitude
在实现文件中有 @dynamic
,并且它们不是保留字(到目前为止据我所知)。我将 RestKit 与 Core Data 一起使用。
2011-11-06 18:12:23.428 Dark Sky Finder[19407:11903] Creating observation site #1
2011-11-06 18:12:23.431 Dark Sky Finder[19407:11903] -[ObservationSite setLatitude:]: unrecognized selector sent to instance 0x735fdf0
2011-11-06 18:12:23.431 Dark Sky Finder[19407:11903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ObservationSite setLatitude:]: unrecognized selector sent to instance 0x735fdf0'
导致错误的代码:
if ([ObservationSite count:nil] == 0)
{
for (int i = 1; i <= 5; i++)
{
NSLog(@"Creating observation site #%i", i);
ObservationSite *site = [ObservationSite object];
site.observationSiteID = [NSNumber numberWithInt:i];
site.name = [NSString stringWithFormat:@"James's Place #%i", i, nil];
site.address = @"Somewhere";
site.latitude = [NSNumber numberWithDouble:30.3605+i];
site.longitude = [NSNumber numberWithDouble:-96.1852+i];
[[[RKObjectManager sharedManager] objectStore] save];
}
}
NSFetchRequest *fetchRequest = [ObservationSite fetchRequest];
NSArray *sites = [[ObservationSite objectsWithFetchRequest:fetchRequest] retain];
for (ObservationSite *site in sites)
{
NSLog(@"Name: %@", site.name);
NSLog(@"Address: %@", site.address);
NSLog(@"Latitude: %@", site.latitude);
NSLog(@"Longitude: %@", site.longitude);
}
ObservationSite
头文件:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <MapKit/MapKit.h>
@interface ObservationSite : NSManagedObject <MKAnnotation>
{
CLLocationCoordinate2D _coordinate;
NSString *_title;
NSString *_subtitle;
}
@property (nonatomic, retain) NSNumber *observationSiteID;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *name;
+ (ObservationSite *)mapAnnotation;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
ObservationSite
实现文件:
#import "ObservationSite.h"
@implementation ObservationSite
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
@dynamic observationSiteID;
@dynamic latitude;
@dynamic longitude;
@dynamic address;
@dynamic name;
+ (ObservationSite *)mapAnnotation {
return [[[self alloc] init] autorelease];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate {
return [self mapAnnotationWithCoordinate:coordinate title:nil subtitle:nil];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title {
return [self mapAnnotationWithCoordinate:coordinate title:title subtitle:nil];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle {
ObservationSite *annotation = [[[self alloc] init] autorelease];
annotation.coordinate = coordinate;
annotation.title = title;
annotation.subtitle = subtitle;
return annotation;
}
@end
I receive the following error. I can't figure out why, since latitude
and longitude
have @dynamic
in the implementation file, and they are not reserved words (as far as I can tell). I am using RestKit with Core Data.
2011-11-06 18:12:23.428 Dark Sky Finder[19407:11903] Creating observation site #1
2011-11-06 18:12:23.431 Dark Sky Finder[19407:11903] -[ObservationSite setLatitude:]: unrecognized selector sent to instance 0x735fdf0
2011-11-06 18:12:23.431 Dark Sky Finder[19407:11903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ObservationSite setLatitude:]: unrecognized selector sent to instance 0x735fdf0'
Code that causes the error:
if ([ObservationSite count:nil] == 0)
{
for (int i = 1; i <= 5; i++)
{
NSLog(@"Creating observation site #%i", i);
ObservationSite *site = [ObservationSite object];
site.observationSiteID = [NSNumber numberWithInt:i];
site.name = [NSString stringWithFormat:@"James's Place #%i", i, nil];
site.address = @"Somewhere";
site.latitude = [NSNumber numberWithDouble:30.3605+i];
site.longitude = [NSNumber numberWithDouble:-96.1852+i];
[[[RKObjectManager sharedManager] objectStore] save];
}
}
NSFetchRequest *fetchRequest = [ObservationSite fetchRequest];
NSArray *sites = [[ObservationSite objectsWithFetchRequest:fetchRequest] retain];
for (ObservationSite *site in sites)
{
NSLog(@"Name: %@", site.name);
NSLog(@"Address: %@", site.address);
NSLog(@"Latitude: %@", site.latitude);
NSLog(@"Longitude: %@", site.longitude);
}
ObservationSite
header file:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <MapKit/MapKit.h>
@interface ObservationSite : NSManagedObject <MKAnnotation>
{
CLLocationCoordinate2D _coordinate;
NSString *_title;
NSString *_subtitle;
}
@property (nonatomic, retain) NSNumber *observationSiteID;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *name;
+ (ObservationSite *)mapAnnotation;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title;
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
ObservationSite
implementation file:
#import "ObservationSite.h"
@implementation ObservationSite
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
@dynamic observationSiteID;
@dynamic latitude;
@dynamic longitude;
@dynamic address;
@dynamic name;
+ (ObservationSite *)mapAnnotation {
return [[[self alloc] init] autorelease];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate {
return [self mapAnnotationWithCoordinate:coordinate title:nil subtitle:nil];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title {
return [self mapAnnotationWithCoordinate:coordinate title:title subtitle:nil];
}
+ (ObservationSite *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle {
ObservationSite *annotation = [[[self alloc] init] autorelease];
annotation.coordinate = coordinate;
annotation.title = title;
annotation.subtitle = subtitle;
return annotation;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
糟糕 - 我在 ObservationSite 标头/实现文件中使用的属性名称与数据模型中的不同。我现在将它们重命名为相同的,并且工作正常。
Whoops — the attribute names I used in the
ObservationSite
header/implementation files were different than in the data model. I renamed them now to be the same, and it works fine.