对 @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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是完整的代码吗?
Client
就像“名为 Client 的类”一样?或者Client
是Client
类的实例吗?这就是为什么实例变量不应以大写字母开头。这很令人困惑。
你的代码可能应该是这样的。
您可以准确地看到什么是类以及什么是变量或属性。
Is this the complete code?
Client
as in "the class named Client"? Or isClient
a instance of the classClient
?That's why instance variables should not start with an uppercase letter. It's confusing.
Your code should probably be something like this.
You can see exactly what is a class and what is a variable or property.