如果使用NsPredicate解析查询后的语句

时间:2015-08-11 19:06:46

标签: ios swift if-statement parse-platform nspredicate

NSPredicateobjectIds是我的fb ID时,我使用senderID进行解析查询以获取所有receiverID

let setPredicate = NSPredicate(format: "senderID = %@ OR receiverID = %@", fbID, fbID)
    var query = PFQuery(className: "Groups", predicate: setPredicate)
    query.addDescendingOrder("updatedAt")

我然后将结果添加到NSMutableDictionary,然后添加到NSMutableArray,如下所示:

query.findObjectsInBackgroundWithBlock
        {(objects:[AnyObject]?, error:NSError?) -> Void in
            if error == nil
            {
                if let objects = objects as? [PFObject]
                {
                    for object in objects
                    {
                        if setPredicate = NSPredicate(format: "senderID", fbID)
                        self.groupDetailsDict.setValue(object.objectForKey("objectId") as! String, forKey: "groupID")
                        self.groupDetailsDict.setValue(object.objectForKey("senderName"), forKey: "senderName")
                        self.groupDetailsDict.setValue(object.objectForKey("receiverName"), forKey: "receiverName")
                        self.groupDetails.addObject(self.groupDetailsDict)
                    }
                }
                else
                {
                    println("Error: \(error) \(error!.userInfo)")
                }
            }
            else
            {
                println(error?.description)
            }
    }

如果我的receiverNamefbID匹配,我只想添加senderId,如果senderNamefbID匹配,我只想添加receiverName 。 我可以在里面使用if else语句吗?如果是,那怎么样?

1 个答案:

答案 0 :(得分:0)

在第一种情况下,不太了解你的逻辑比较ID和ID,第二种情况下ID是ID,但如果我们假设test是我们的目标数组(对不起,请不要非常明白groupDetailsDict的意思,也许你可以解释一下)代码看起来像(有了所有适当的快速解开):

if let senderId = object["senderId"] as? String, receiverName = object["receiverName"] as? String where senderId == fbID {
    test.append(receiverName)
} else if let receiverName = object["receiverName"] as? String, senderName = object["senderName"] as? String where receiverName == fbID {
    test.append(senderName)
}