AnyObject上的算术运算失败

时间:2015-06-28 09:23:24

标签: ios swift parse-platform

我在iOS Swift中使用Parse做一个应用程序。每次查看产品时我都需要增加1。以下是我的代码:

var prodQuery = PFQuery(className: "prodDet")
        let id = prodId[currprod] as String
        prodQuery.whereKey("objectId", equalTo:id)

        prodQuery.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                for object in objects {

                    var key1: Int = object["key1Total"]! as Int
                    var key1New: Int = key1 + 1

                    object["key1Total"] = key1New

                }
            }
        }

上面的代码给出了错误“@lvalue $ T5与AnyObject ...不同”。我在这里处理AnyObject对象的方式有什么不对;我们如何在Parse AnyObjects上进行算术计算?

有人可以解释一下吗?

1 个答案:

答案 0 :(得分:0)

我把它整理出来,似乎我应该将对象强制转换为PFObject并使用object.setValue。以下是工作代码:

    var prodQuery = PFQuery(className: "prodDet")
let id = prodId[currprod] as String
prodQuery.whereKey("objectId", equalTo:id)

prodQuery.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            for object in objects as [PFObject] {
                var keyVal = object["key1Total"]! as Int
                var keyNew = keyVal + 1

                object.setValue(keyNew, forKey: "key1Total")

                object.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                        if (success) {
                            //
                        } else {
                            //
                    }
                }
            }
        }
    }

    object.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
            if (success) {
                //
                } else {
                //
            }
        }
    }
}

}