对@dynamic访问器的引用无法编译

时间:2011-04-16 18:52:08

标签: objective-c core-data dynamic

这是我第一次使用@dynamic关键字。我正在尝试使用核心数据。我有一个类引用NSManagedObject类型的对象。 NSManagedObject类型的对象是由XCode从我设置的现有核心数据实体自动创建的。

由于在'Client.FirstName'开头的行发生错误,代码将无法编译。请参阅该行评论中的注释。

由于引用是动态访问器,我很惊讶编译器会将此标记为错误引用。它看起来很简单,但我显然遗漏了一些东西。我不确定是什么问题。

代码是这样的:

aTestClass.h

#import <Foundation/Foundation.h>    

@interface aTestClass : NSObject {

}

- (void) foo;

@end

aTestClass.m

#import "aTestClass.h"
#import "Client.h"

@implementation aTestClass

- (void) foo {
    Client.FirstName = @"CompilerFail";  // Fails here: Property 'FirstName' not found in object of type 'Client'
}

@end

,客户端代码是这个NSManagedObject,由XCode 4自动生成。

Client.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Client : NSManagedObject {
//@private
}
@property (nonatomic, retain) NSString * FirstName;
@property (nonatomic, retain) NSString * LastName;
@property (nonatomic, retain) NSString * Company;
@property (nonatomic, retain) NSSet* Jobs;

@end

Client.m

#import "Client.h"

@implementation Client
@dynamic FirstName;
@dynamic LastName;
@dynamic Company;
@dynamic Jobs;

- (void)addJobsObject:(NSManagedObject *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Jobs"] addObject:value];
    [self didChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeJobsObject:(NSManagedObject *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Jobs"] removeObject:value];
    [self didChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addJobs:(NSSet *)value {    
    [self willChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Jobs"] unionSet:value];
    [self didChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeJobs:(NSSet *)value {
    [self willChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Jobs"] minusSet:value];
    [self didChangeValueForKey:@"Jobs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}

@end

1 个答案:

答案 0 :(得分:2)

这是完整的代码吗?

  

Client.FirstName = @“CompilerFail”;

Client和“名为Client的类”一样?或者是ClientClient的实例?

这就是为什么实例变量不应该以大写字母开头。这令人困惑。

您的代码应该是这样的。

Client *someClient = ...
someClient.firstName = @"Foo";

您可以准确地看到什么是类,什么是变量或属性。