gpt4 book ai didi

iphone - CoreData关系澄清: How to effectively set an inverse relationship

转载 作者:行者123 更新时间:2023-12-03 21:21:44 26 4
gpt4 key购买 nike

我的 CoreData 关系设置如下:(抱歉,我是 stackoverflow 的新手,所以我必须用 ascii 来绘制它)。

<小时/>

故事(对象)

  • 属性:创建日期,订单,

  • 关系:句子(一对多与句子)

句子(宾语)

  • 属性:图像、顺序、文本

  • 关系:belongsTo(与故事一对一)

<小时/>

注释:

  • 句子关系是一个 NSSet
  • belongsTo 关系是一个故事

简单来说,一个Story有很多Sentence,但一个Sentence只能属于一个Story。

我在有效设置belongsTo属性时遇到了麻烦 - 而且我知道我有点缺少范例。我担心当我设置 ownTo 时,我将使用它自己的 NSSet 创建另一个 Story 对象,从而创建一个递归循环。无论如何,我的问题似乎是有效设置每个句子的belongsTo 属性/关系。

将我的数据插入 CoreData 存储库的代码如下所示:

-(void)addStoryItemAction:(Sentence*)sentence{

[self dismissModalViewControllerAnimated:YES];
Story *story = [[Story alloc] initWithSentence: sentence];

sentence.belongsTo = story;

//crash and burn here.

Story *storySetter = (Story*) [NSEntityDescription insertNewObjectForEntityForName:@"Story" inManagedObjectContext:managedObjectContext];
[storySetter setSentences: story.sentences];
[storySetter setCreationDate: story.creationDate];
[storySetter setOrder: story.order];

NSError *error;

BOOL isSaved = [managedObjectContext save:&error];
NSLog(@"isSaved? %@", (isSaved ? @"YES" :@"NO ") );

if (!isSaved) {
NSLog(@"%@:%s Error saving context: %@", [self class], _cmd, [error localizedDescription]);
return;
}

}

运行时出现以下错误:

-[Sentence setBelongsTo:]:无法识别的选择器发送到实例 0x5b5aa00

我知道我在某个地方错过了重点 - 有人可以澄清我出错的地方,以便我可以有效地实现此 CoreData 集的插入吗?

仅供引用:句子.h:

 #import <Foundation/Foundation.h>
#import "Story.h"

@class Story;

@interface Sentence : NSManagedObject {
}
@property (assign) NSString *text;
@property (assign) NSString *image;
@property (assign) NSInteger *order;
@property (nonatomic, retain) Story *belongsTo;

- (id)initWithContent:(NSString*)sentenceText image:(NSString*)sentenceImage order: (NSInteger*)sentencePosition;
@end

句子.m:

 #import "Sentence.h"
@implementation Sentence
@synthesize text;
@synthesize image;
@synthesize order;
@dynamic belongsTo;

- (id)initWithContent:(NSString*)sentenceText image:(NSString*)sentenceImage order: (NSInteger*)sentencePosition{
[self setText: sentenceText];
[self setImage: sentenceImage];
[self setOrder: sentencePosition];
return self;
}
@end

故事.h

 #import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Sentence.h"
@class Sentence;
@interface Story : NSManagedObject {
}
@property (assign) NSSet *sentences;
@property (assign) NSDate *creationDate;
@property (assign) NSInteger *order;

- (id)initWithSentence:(Sentence*)sentence;
@end

故事.m

 #import "Story.h"
@implementation Story
@synthesize sentences;
@synthesize creationDate;
@synthesize order;

- (id)initWithSentence: (Sentence*) sentence{

NSLog(@"initWithSencence: sentence: %@", sentence);
sentences = [[NSSet alloc] initWithObjects:sentence, nil];
//[sentences setByAddingObject:sentence];
[self setSentences:sentences];
NSLog(@"sentences (in story object): %@", sentences);
creationDate = [NSDate date];
[self setCreationDate:creationDate];
//later expansion, position should be max(position of all stories) ++
[self setOrder:0];

return self;
}

@end

最佳答案

我有点困惑为什么你直接在 Story 上调用 alloc/init 。我怀疑,如果您在代码中的其他地方做同样的事情(您应该发布给我们看),那么传递给 addStoryItemAction: 的“句子”对象可能不是正确初始化的 NSManagedObject。

您遇到的错误意味着底层类没有正确实现该属性“belongsTo”的访问器,因此您的初始化代码中似乎出现了问题。您应该使用:

NSEntityDescription *entityFromModel = [[aModel entitiesByName] valueForKey:@"Sentence"];
Sentence *sentence = (Sentence *)[[NSManagedObject alloc] initWithEntity:entityFromModel insertIntoManagedObjectContext:context];

关于iphone - CoreData关系澄清: How to effectively set an inverse relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3966682/

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