我如何在swift / parse中检索与userid匹配的对象?

时间:2015-12-24 05:32:46

标签: ios swift

如何在解析swift时检索与特定用户ID位于同一行的项目? 它们都属于同一个自定义类

现在代码检索所有项目。我想根据用户ID /用户名

过滤它们
extension reservedCust {
class func userWithUserId(userid userid : String) -> reservedCust? {
    let query = reservedCust.query();
    query?.whereKey("userid", equalTo: userid);
    var userssss : [PFObject]?

    do {
        userssss = try query?.findObjects();
    }
    catch {
        return nil;
    }

    if userssss?.count > 0 {
        return userssss?[0] as? reservedCust;
    }
    return nil;
}

}

class itemsTableViewController: UITableViewController {

var itemList = [String]()
var userid = [String]()
var names = [String]()




override func viewDidLoad() {
    super.viewDidLoad()

    if let user = reservedCust.userWithUserId(userid : "your UserID") {
        //do you stuff with retrieved user


    let query = PFQuery(className:"reservedCust")
    query.findObjectsInBackgroundWithBlock {

        (objects, error) -> Void in

        if error == nil {


            if let objects = objects {

                for object in objects {


                    if let item = object["item"] as? String {

                        self.itemList.append((object["item"] as? String)!)

                    }



                }


            }

            print(self.itemList)
            self.tableView.reloadData()


        }else {

        }
    }

}


}

1 个答案:

答案 0 :(得分:0)

extension reservedCust {
    class func userWithUserId(userID userID : String) -> reservedCust? {
        let query = reservedCust.query();
        query?.whereKey("userID", equalTo: userID);
        var userssss : [PFObject]?

        do {
            userssss = try query?.findObjects();
        }
        catch {
            return nil;
        }

        if userssss?.count > 0 {
            return userssss?[0] as? reservedCust;
        }
        return nil;
    }
}

<强> Assumtions: 1)您应该粘贴在扩展代码上方,在您的身体外侧。 2)您的userID列名称是userID,请用您的列名替换行query?.whereKey("userID", equalTo: userID);的字符串“userID”,因此它将为query?.whereKey("your column name for userID field", equalTo: userID);

使用:

if let user = reservedCust.userWithUserId(userID : "your UserID") {
       //do you stuff with retrieved user
}

viewDidLoad将是:

override func viewDidLoad() {
        super.viewDidLoad()

        if let user = reservedCust.userWithUserId(userid : "your UserID") {
            //do you stuff with retrieved user

            //your core logic depends on item is string or array or whatever
            if let item = user["item"] as? String {

                    self.itemList.append((user["item"] as? String)!)

            }


           print(self.itemList)
           self.tableView.reloadData(

            }else {

            }
        }