這是我第一次使用@dynamic關鍵字。我正在嘗試使用核心數據。我有一個類引用NSManagedObject類型的對象。 NSManagedObject類型的對象是由XCode從我設置的現有核心數據實體自動創建的。引用@dynamic訪問器無法編譯
由於在'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
如果您按照語言的命名約定,它會有很大的幫助;課程以大寫字母開頭。方法和屬性以小寫字母開頭。 – bbum 2011-04-17 05:17:38