ios - 将 NSArray 属性添加到子类 PFObject

标签 ios parse-platform

我有两个子类 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/

相关文章:

ios - 如何使用 Swift 将注册页面添加到我的 Parse 应用程序中?

ios - PresentModalViewController 一旦应用程序从 Framework 启动

ios - 从 Storyboard 中的外部 xib 文件加载 View

ios - 具有两种不同颜色文本的 UILabel

ios - 无法更新解析用户详细信息 - Swift

android - 我如何判断这是否是用户的第一次 session ? Parse.com(安卓)

ios - 如何在obj-c中存储动画?

ios - UIButton 子类标题更改

ios - 从 Parse 查询中访问 NSArray

ios - 如何在 Parse 中获取具有对象 ID 数组的对象