简体   繁体   English

在AnyObject上的算术运算失败

[英]Arithmetic operation fails on AnyObject

I'm doing an app in iOS Swift with Parse. 我正在使用Parse在iOS Swift中开发应用程序。 I need to increment 1 every time a product is viewed. 每次查看产品时,我需要增加1。 Following is my code to do that: 以下是我执行此操作的代码:

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

                }
            }
        }

The above code gives error "@lvalue$T5 is not identical to AnyObject...". 上面的代码给出错误“ @ lvalue $ T5与AnyObject ...不相同”。 Is anything wrong the way I'm dealing with AnyObject object here; 我在这里处理AnyObject对象的方式有什么问题吗? How do we do arithmatic calculation on Parse AnyObjects? 我们如何在解析AnyObjects上进行算术计算?

Could somebody please shed some light? 有人可以说明一下吗?

I sorted it out, seems like I should cast the object as PFObject and use object.setValue. 我整理了一下,似乎我应该将该对象转换为PFObject并使用object.setValue。 The following is the working code: 以下是工作代码:

    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 {
                //
            }
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 卡在算术运算错误上 - Stuck on a arithmetic operation error 从“字符串?”投射 到不相关类型&#39;[[String:AnyObject]]&#39;总是失败 - Cast from 'String?!' to unrelated type '[[String : AnyObject]]' always fails 从[String:AnyObject]强制转换为不相关的类型NSMutableDictionary始终失败警告 - cast from [String:AnyObject] to unrelated type NSMutableDictionary always fails Warning 值超出目标c的算术运算 - Arithmetic operation of value that exceeds limit in objective c “ AnyObject”与“ [NSObject:AnyObject]”不同 - 'AnyObject' is not identical to '[NSObject : AnyObject]' `[AnyObject]`到NSMutableArray - `[AnyObject]` to NSMutableArray AnyObject to Array? - AnyObject to Array? 用Monotouch修剪视频失败,并显示“操作无法完成” - Trimming video with Monotouch fails with “The operation could not be completed” (键:AnyObject,值:AnyObject)不能转换为&#39;Dictionary <String, AnyObject> - (key: AnyObject, value: AnyObject) is not convertible to 'Dictionary<String, AnyObject> [String:AnyObject]如何成为AnyObject,但是[String:AnyObject?]不能? - How can [String: AnyObject] be an AnyObject, but [String: AnyObject?] can't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM