我在Xcode 7 beta 6中使用核心數據,並且爲每個實體生成類別和託管對象子類。問題是,當我嘗試利用從我的模型中的屬性創建的屬性時,出現「使用未聲明的標識符」錯誤。我的印象是,我應該把自定義行爲放在託管對象的子類中,但是我不清楚如何使用託管對象子類中的類別的屬性,所以我將自定義行爲放在類別如下所示。我覺得我只是錯過了一個導入聲明,但我不確定。我瞭解我正在使用測試版軟件。使用未聲明的標識符核心數據
思想+ CoreDataProperties.h:
#import "Thought.h"
NS_ASSUME_NONNULL_BEGIN
@interface Thought (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *objectId;
@property (nullable, nonatomic, retain) id recordId;
@property (nullable, nonatomic, retain) Collection *parentCollection;
@property (nullable, nonatomic, retain) NSNumber *placement;
@property (nullable, nonatomic, retain) NSString *text;
@property (nullable, nonatomic, retain) NSString *extraText; // allows for extra description text to be set. Should be in smaller print than headline text and should only appear as an option in text != nil
@property (nullable, nonatomic, retain) NSSet<Photo *> *photos;
@property (nullable, nonatomic, retain) id location; // place a CLLocation here
@property (nullable, nonatomic, retain) id tags; // place an NSArray here
@property (nullable, nonatomic, retain) NSDate *creationDate;
#pragma mark - Initializers
/*!
@abstract this method converts a CKRecord into a Thought object
@discussion parentCollection will still be nil after this method executes
*/
-(nullable instancetype) initWithRecord: (nonnull CKRecord *) record;
/*!
@abstract this method converts a CKRecord into a Thought object. photos set is not populated
*/
-(nullable instancetype)initWithRecord: (nonnull CKRecord *) record collection: (nonnull Collection *) collection;
/*!
@abstract Creates a new Thought object with generic recordId, objectId, placement, and photos array
@discussion parentCollection will still be nil after this method executes
*/
-(nullable instancetype) init;
… other methods
@end
@interface Thought (CoreDataGeneratedAccessors)
- (void)addPhotosObject:(Photo *)value;
- (void)removePhotosObject:(Photo *)value;
- (void)addPhotos:(NSSet<Photo *> *)values;
- (void)removePhotos:(NSSet<Photo *> *)values;
@end
NS_ASSUME_NONNULL_END
思想+ CoreDataProperties.m:
#import "Thought+CoreDataProperties.h"
@implementation Thought (CoreDataProperties)
@dynamic creationDate;
@dynamic extraText;
@dynamic location;
@dynamic objectId;
@dynamic placement;
@dynamic recordId;
@dynamic tags;
@dynamic text;
@dynamic parentCollection;
@dynamic photos;
-(nullable instancetype) init {
self = [super init];
if (self) {
// THIS IS WHERE I GET MANY ERROR FOR USE OF UNDECLARED IDENTIFIER
_objectId = [IdentifierCreator createId];
_recordId = [[CKRecord alloc] initWithRecordType:THOUGHT_RECORD_TYPE zoneID:[[CKRecordZone alloc] initWithZoneName:ZONE_NAME].zoneID].recordID;
_photos = [NSArray new];
_placement = [NSNumber numberWithInt:0];
_creationDate = [NSDate date];
}
return self;
}
-(instancetype) initWithRecord:(nonnull CKRecord *)record {
self = [super init];
if (self) {
_objectId = [record objectForKey:OBJECT_ID_KEY];
_recordId = [record recordID];
_text = [record objectForKey:TEXT_KEY];
_extraText = [record objectForKey:EXTRA_TEXT_KEY];
_location = [record objectForKey:LOCATION_KEY];
_photos = [NSSet new];
_tags = [record objectForKey:TAGS_KEY];
_placement = [record objectForKey:PLACEMENT_KEY];
_creationDate = record.creationDate;
}
return self;
}
-(instancetype) initWithRecord:(CKRecord *)record collection:(Collection *)collection {
self = [self initWithRecord:record];
self.parentCollection = collection;
return self;
}
Thought.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Frameworks.h" // includes Frameworks I'm using and some string constants
#import "ForFundamentals.h" // includes mostly string constants
#import "Photo.h"
#import "Collection.h"
@class Collection, Photo;
NS_ASSUME_NONNULL_BEGIN
@interface Thought : NSManagedObject
// I think I should put method declarations here
@end
NS_ASSUME_NONNULL_END
#import "Thought+CoreDataProperties.h"
Thought.m:
#import "Thought.h"
#import "Collection.h"
#import "Photo.h"
@implementation Thought
// I think I should put method implementations here
@end
錯誤的方式 - 屬性定義應該在子類int中erface /實現文件,方法應該在類別文件中。這個想法是,您可以在優化模型時反覆使用Xcode的「創建NSManagedObject子類」,它將創建/覆蓋子類定義文件,而不覆蓋類別文件中的方法定義。抱歉...除了你的方法重寫NSManagedObject方法,所以也需要在子類文件中。 – pbasdf
@pbasdf感謝您的快速回答!我認爲你是對的,這是我想象的方式。但是,當核心數據生成類別時,他們將屬性放在類別文件中,這使我相信應該放置新的位置屬性。 –
所以我通過Xcode 7重新生成了子類和類別,並且我試圖向兩個方法中添加一個方法,但實際上它不會給我任何代碼完成,因此我認爲Xcode創建的類可能會出錯。通常,當我認爲這是Apple代碼中的錯誤時,我通常是錯的。但是,這是測試版軟件。 –