开发者

Reference to @dynamic accessor fails to compile

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:NSKeyValueMinusSetMutatio开发者_高级运维n 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜