如何在Swift中更新Parse中的数组

时间:2015-08-28 09:39:53

标签: ios swift parse-platform

我的Parse类是Groups。

在那个类中,我有很多列,包括createdBy:String和GroupMembersList:Array

对于一行,我有以下值:

GroupMembersList = ["Arun", "Daniel", "Jo"]

现在我如何从我的快速代码中add removeupdate这个群组的成员?

到目前为止,我知道这一点:

var query:PFQuery = PFQuery(className: "Groups")
query.whereKey("CreatedBy", equalTo: "Arun")

1 个答案:

答案 0 :(得分:1)

您可以向群组添加属性,并像使用常规数组一样处理它:

extension Group {
    var membersList: [String] {
        get {
            if let x = self["GroupMembersList"] as? [String] { return x }
            return []
        }
        set(val) {
            self["GroupMembersList"] = val
        }
    }
}

修改:可以使用Parse(docs)创建自定义PFObject子类:

class Group: PFObject, PFSubclassing {
    static func parseClassName() -> String {
        return "Group"
    }

    var membersList: [String] {
        get {
            if let x = self["GroupMembersList"] as? [String] { return x }
            return []
        }
        set(val) {
            self["GroupMembersList"] = val
        }
    }
}

AppDelegete之前Parse.setApplicationId

Group.registerSubclass()

之后你可以使用你的子类:

let group = Group(objectId: "<group_id>")
group.membersList = ["some", "members", "here"]
let first = group.membersList[0]
group.saveInBackgroundWithBlock { (success, error) in
    if (success) {
        // The object has been saved.
    } else {
        // There was a problem, check error.description
    }
}