我有两个子类 PFObject,如下所示:
@interface BQQuestion : PFObject<PFSubclassing>
@property (retain) NSString *user;
@property (retain) NSString *questionText;
@property (retain) NSArray *answers; //this is intended to hold an array of BQAnswers
+ (NSString *)parseClassName;
@end
@interface BQAnswer : PFObject<PFSubclassing>
@property (retain) NSString *user;
@property (retain) BQQuestion *question;
@property (retain) NSString *answerText;
@property int votes;
+(NSString *)parseClassName;
@end
我尝试使用以下代码将 BQAnswers 添加到 BQQuestion 的答案属性:
BQQuestion *t = [BQQuestion object];
t.questionText = @"Another test";
BQAnswer *one = [BQAnswer object];
one.answerText = @"Very nice.";
BQAnswer *two = [BQAnswer object];
two.answerText = @"Good.";
t.answers = @[one, two];
[t saveInBackground];
t、一和二都保存到 Parse 云中,但没有为 BQQuestion.answers 生成列,因此不会保存关联。我在这里弄错了什么?
任何帮助将不胜感激。谢谢!
最佳答案
指向 PFObject 数组的指针是 PFRelation。为此,您需要在 Question 对象中创建一个列,其中包含指向 Answer 对象的关系。我假设您的解析类称为“问题”,并且添加的包含指向答案的关系的列称为“答案”
BQQuestion *question = [PFObject objectWithClassName:@"Question"];
PFRelation *answers = [t relationForKey:@"answers"];
for (PFObject *answer in answers) {
[question add:answer];
}
[question saveInBackground];
检索答案对象:
BQQuestion *question = //assume you have the question object;
PFRelation *answers = [question relationForKey:@"answers"];
PFQuery *query = [answers query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all the answers to that question
}];
关于ios - 将 NSArray 属性添加到子类 PFObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121300/