NSManagedObject getter/setter 无法识别的选择器错误

发布于 2024-12-13 18:21:32 字数 3724 浏览 1 评论 0原文

我收到以下错误。我不明白为什么,因为 latitudelongitude 在实现文件中有 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-12-20 18:21:32

糟糕 - 我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文