Ios Swift:无法分配给类型为';任何物体';

Ios Swift:无法分配给类型为';任何物体';,ios,xcode,swift,parse-platform,Ios,Xcode,Swift,Parse Platform,我搜索了一下,但没有找到熟悉的答案,所以 我将编写一个类来处理更新、添加、获取和删除等解析方法 func updateParse(className:String, whereKey:String, equalTo:String, updateData:Dictionary<String, String>) { let query = PFQuery(className: className) query.whereKey(whereKey, equalTo: e

我搜索了一下,但没有找到熟悉的答案,所以

我将编写一个类来处理更新、添加、获取和删除等解析方法

func updateParse(className:String, whereKey:String, equalTo:String, updateData:Dictionary<String, String>) {

    let query = PFQuery(className: className)

    query.whereKey(whereKey, equalTo: equalTo)
    query.findObjectsInBackgroundWithBlock {(objects, error) -> Void in
        if error == nil {
            //this will always have one single object
            for user in objects! {
                //user.count would be always 1
                for (key, value) in updateData {

                    user[key] = value //Cannot assign to immutable expression of type 'AnyObject?!'

                }

                user.saveInBackground()
            } 

        } else {
            print("Fehler beim Update der Klasse \(className) where \(whereKey) = \(equalTo)")
        }
    }

}
错误消息说,您正在尝试更改不可变对象,这是不可能的

默认情况下,在闭包中声明为方法参数或返回值的对象是不可变的

要使对象可变,请在方法声明中添加关键字
var
,或添加一行以创建可变对象

在默认情况下,重复循环中的索引变量也是不可变的

在这种情况下,插入一行以创建可变副本,并将索引变量声明为可变

在枚举对象时小心更改对象,这可能会导致意外行为

...
query.findObjectsInBackgroundWithBlock {(objects, error) -> Void in
    if error == nil {
        //this will always have one single object
        var mutableObjects = objects
        for var user in mutableObjects! {
            //user.count would be always 1
            for (key, value) in updateData {

                user[key] = value
...

在swift中,许多类型被定义为
struct
s,默认情况下是不可变的

我在做这件事时也犯了同样的错误:

protocol MyProtocol {
    var anInt: Int {get set}
}

class A {

}

class B: A, MyProtocol {
    var anInt: Int = 0
}
在另一个班级:

class X {

   var myA: A

   ... 
   (self.myA as! MyProtocol).anInt = 1  //compile error here
   //because MyProtocol can be a struct
   //so it is inferred immutable
   //since the protocol declaration is 
   protocol MyProtocol {...
   //and not 
   protocol MyProtocol: class {...
   ...
}
所以一定要有

protocol MyProtocol: class {

进行此类转换时

也会影响重复循环中的索引变量。我更改了postNice查找-为我节省了大量时间ta。这已更改-建议使用
协议MyProtocol:AnyObject{
而不是这是我发现唯一有意义的解释。谢谢!!@ZpaceZombor:谢谢我的问题已通过您的评论得到解决:)
protocol MyProtocol: class {