ios Swift:无法分配给“AnyObject?!”类型的不可变表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32093692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:19:59  来源:igfitidea点击:

Swift: Cannot assign to immutable expression of type 'AnyObject?!'

iosxcodeswiftparse-platform

提问by PhilHarmonie

I searched, but I didn't find a familiar answer, so...

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

I am about to program a class to handle parse methods like updating, adding, fetching and deleting.

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

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)")
        }
    }

}

As I am about to learn swift at the moment, I would love to get an answer with a little declaration, so that I would learn a little bit more.

由于我现在即将学习 swift,我很想得到一个小小的声明的答案,这样我就会学到更多。

btw: I later call this method like this:

顺便说一句:我后来这样称呼这个方法:

parseAdd.updateParse("UserProfile", whereKey: "username", equalTo: "Phil", updateData: ["vorname":self.vornameTextField!.text!,"nachname":self.nachnameTextField!.text!,"telefonnummer":self.telefonnummerTextField!.text!])

采纳答案by vadian

The error message says, you're trying to change an immutable object, which is not possible.

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

Objects declared as method parameters or return values in closures are immutable by default.

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

To make the object mutable either add the keyword varin the method declaration or add a line to create a mutable object.

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

Also index variables in repeat loops are immutable by default.

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

In this case a line is inserted to create a mutable copy and the index variable is declared as mutable.

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

Be careful to change objects while being enumerated, this could cause unexpected behavior

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

...
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
...

回答by ZpaceZombor

In swift a lot of types are defined as structs, which are immutable by default.

在 swift 中,许多类型被定义为structs,默认情况下它们是不可变的。

I had the same error doing this :

我在这样做时遇到了同样的错误:

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

class A {

}

class B: A, MyProtocol {
    var anInt: Int = 0
}

and in another class :

在另一堂课中:

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

so be sure to have

所以一定要

protocol MyProtocol: class {

when doing such casting

在做这样的铸造时