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

发布于 2024-11-01 19:07:12 字数 2773 浏览 1 评论 0原文

这是我第一次使用@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.m

#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.h

#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

This is my first use of the @dynamic keyword. I'm trying to use core data. I have a class referring to an object of NSManagedObject type. The object of NSManagedObject type was automatically created by XCode from an existing core data entity that I had set up.

The code will not compile because of an error at the line beginning 'Client.FirstName'. See the note in the comment on that line.

Since the reference is to a dynamic accessor, I am surprised that the compiler would flag this as a bad reference. It looks pretty simple, but I'm obviously missing something. I'm not sure what is wrong.

The code is this:

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

and the Client codes is this NSManagedObject, automatically generated by 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

会傲 2024-11-08 19:07:12

这是完整的代码吗?

Client.FirstName = @"CompilerFail";

Client 就像“名为 Client 的类”一样?或者 ClientClient 类的实例吗?

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

你的代码可能应该是这样的。

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

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

Is this the complete code?

Client.FirstName = @"CompilerFail";

Client as in "the class named Client"? Or is Client a instance of the class Client?

That's why instance variables should not start with an uppercase letter. It's confusing.

Your code should probably be something like this.

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

You can see exactly what is a class and what is a variable or property.

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