无法将'NSObject - >() - > PostFeed'类型的值转换为预期的参数类型'AnyObject?'

时间:2022-07-11 15:21:38

I'm adding a UISwitch in the following way:

我正在以下列方式添加UISwitch:

  let anonSwitch : UISwitch = {
   let mySwitch = UISwitch()
    mySwitch.on = false
    mySwitch.setOn(false, animated: false);
    mySwitch.tintColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
    mySwitch.addTarget(self, action: #selector(handleAnonSwitch), forControlEvents: .ValueChanged)

    return mySwitch
}()

Now I'm getting the following error message on the self keyword in mySwitch.addTarget :

现在我在mySwitch.addTarget中的self关键字上收到以下错误消息:

 Cannot convert value of type 'NSObject -> () -> PostFeed' to expected argument type 'AnyObject?'

I use self in all my other addTarget functions for UIButton and I never encounter this error

我在UIButton的所有其他addTarget函数中使用self,我从未遇到过这个错误

1 个解决方案

#1


12  

Change your let into a lazy var.

将你的let变成懒惰变种。

I'm not sure exactly what the compiler is thinking (or where its crazy error message is coming from), but my guess is this:

我不确定编译器到底在想什么(或者它的疯狂错误信息来自哪里),但我猜是这样的:

In Swift, variables which are let have to be initialized before you can use self. One reason for this is that the compiler can't verify that addTarget(action:forControlEvents:) isn't going to try to call anonSwitch() on whatever it gets for target, or do something else, like access a different variable that would be initialized after anonSwitch, that depends on initialization having completed for this object.

在Swift中,在使用self之前必须初始化变量。这样做的一个原因是编译器无法验证addTarget(action:forControlEvents :)是否会尝试在目标获取的任何内容上调用anonSwitch(),或者执行其他操作,例如访问其他变量在anonSwitch之后初始化,这取决于为此对象完成的初始化。

Using lazy var means that the compiler can verify that the value assigned to anonSwitch won't be accessed before self is a valid object, because it won't be possible to call anonSwitch until all other members of the class have been initialized.

使用lazy var意味着编译器可以验证在self是有效对象之前不会访问分配给anonSwitch的值,因为在类的所有其他成员都已初始化之前,将无法调用anonSwitch。

#1


12  

Change your let into a lazy var.

将你的let变成懒惰变种。

I'm not sure exactly what the compiler is thinking (or where its crazy error message is coming from), but my guess is this:

我不确定编译器到底在想什么(或者它的疯狂错误信息来自哪里),但我猜是这样的:

In Swift, variables which are let have to be initialized before you can use self. One reason for this is that the compiler can't verify that addTarget(action:forControlEvents:) isn't going to try to call anonSwitch() on whatever it gets for target, or do something else, like access a different variable that would be initialized after anonSwitch, that depends on initialization having completed for this object.

在Swift中,在使用self之前必须初始化变量。这样做的一个原因是编译器无法验证addTarget(action:forControlEvents :)是否会尝试在目标获取的任何内容上调用anonSwitch(),或者执行其他操作,例如访问其他变量在anonSwitch之后初始化,这取决于为此对象完成的初始化。

Using lazy var means that the compiler can verify that the value assigned to anonSwitch won't be accessed before self is a valid object, because it won't be possible to call anonSwitch until all other members of the class have been initialized.

使用lazy var意味着编译器可以验证在self是有效对象之前不会访问分配给anonSwitch的值,因为在类的所有其他成员都已初始化之前,将无法调用anonSwitch。