gpt4 book ai didi

ios - 使用未声明的标识符核心数据

转载 作者:行者123 更新时间:2023-11-29 01:42:39 24 4
gpt4 key购买 nike

我正在 Xcode 7 beta 6 中使用核心数据,我刚刚为每个实体生成了类别和托管对象子类。问题是,当我尝试利用模型中的属性创建的属性时,我收到“使用未声明的标识符”错误。我的印象是我应该将自定义行为放入生成的托管对象子类中,但是我不清楚如何使用托管对象子类中类别的属性,因此我将自定义行为放入类别如下所示。我觉得我只是错过了一个导入声明,但我不确定。我知道我正在使用测试版软件。

核心数据模型: enter image description here

Thought+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

Thought+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"

思想.m:

#import "Thought.h"
#import "Collection.h"
#import "Photo.h"

@implementation Thought

// I think I should put method implementations here

@end

最佳答案

NSManagedObject 的子类在 awakeFromInsertawakeFromFetch 中进行初始化。不要覆盖 init 或实现 initWith...。您必须等到对象被实例化,并在 NSManagedObjectContext 中处于事件状态,然后才能设置其属性。

不要将集合实例分配给对应于 Core Data 关系(即 _photosparentCollection)的 ivar。Core Data 会在您插入或获取对象。

代替您的 init 方法,重新考虑您的方法。编写方法 insertInManagedObjectContext:withSKRecord。该方法调用 -insertNewObjectForEntityForName:@"Thought"inManagedObjectContext:foo,返回 Thought 的实例。现在,有了那个 istance,设置 objecgtID、recordID 等——但是使用您的访问器,而不是直接敲打实例变量。

关于ios - 使用未声明的标识符核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com