ios Swift - ViewController 类型的值没有成员 *functionName*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34470834/
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 08:28:35  来源:igfitidea点击:

Swift - Value of Type ViewController has no member *functionName*

iosswiftfunction

提问by SergeH

In my app i have several scenarios, each showing a different UIAlertController, so i created a function to show this alert, but i can't seem to call self.Function inside the "okAction" . I'm getting this error :

在我的应用程序中,我有几个场景,每个场景都显示不同的UIAlertController,所以我创建了一个函数来显示此警报,但我似乎无法在 "okAction" 中调用 self.Function 。我收到此错误:

Value of type 'ViewController'has no member 'doAction'

类型的值'ViewController'没有成员'doAction'

Here's the code :

这是代码:

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : () )
{
    let refreshAlert = UIAlertController(title: titleOfAlert, message: messageOfAlert, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) {
        UIAlertAction in

        self.doAction()
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default) {
        UIAlertAction in
    }

    refreshAlert.addAction(okAction)
    refreshAlert.addAction(cancelAction)

    self.presentViewController(refreshAlert, animated: true, completion: nil)
}

Here's one of the functions that i'm calling :

这是我正在调用的功能之一:

func changeLabel1()
{
    label.text = "FOR BUTTON 1"
}

How can i solve it ?

我该如何解决?

回答by luk2302

  1. remove the selfin front of doAction()since you do not call the action on the object self.

  2. If you do that the compiler will tell you
    Invalid use of '()' to call a value of non-function type '()'. That is the case because doActionis no function but an empty tuple. A function has input parameters and a return type. Therefore the type of doActionshould be () -> Void- it takes no input and returns Void, i.e. does not return anything.

  1. 删除self前面的,doAction()因为您没有在对象 self 上调用操作。

  2. 如果你这样做,编译器会告诉你
    Invalid use of '()' to call a value of non-function type '()'。情况就是这样,因为doAction它没有函数,而是一个空元组。函数具有输入参数和返回类型。因此类型doAction应该是() -> Void- 它不需要输入并返回Void,即不返回任何东西。

The code should be like this:

代码应该是这样的:

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : () -> Void ) {
    ...
    let okAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) { action in
        doAction()
    }
    ...
}

If you want to pass the actionto the doActionmethod you would have to change the type to (UIAlertAction) -> Voidand call it via doAction(action).

如果要将 传递actiondoAction方法,则必须将类型更改为(UIAlertAction) -> Void并通过 调用它doAction(action)

回答by Nikita Kurtin

From your code doActionis the third parameter of function showAlertController.

从您的代码doAction是函数showAlertController的第三个参数 。

so First: change doAction : ()to doAction: ()->()It means that doAction is a Closurewithout parameters and empty return.

所以首先:将doAction :()改为doAction: ()->()表示 doAction 是一个没有参数且返回空的闭包

Second: the call shouldn't be self.doAction()but just doAction()Because its a parameter and not an instance variable

第二:调用不应该是self.doAction()而应该是doAction()因为它是一个参数而不是一个实例变量

回答by the_critic

I think the correct way to specify a closure is like this:

我认为指定闭包的正确方法是这样的:

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : (() -> Void) ){

   // and then call it like that
   doAction()

}

The key thing here is that you cannot call this.doAction()because the parameter you pass into the function is not a property on your controller, right ?

这里的关键是你不能调用,this.doAction()因为你传递给函数的参数不是控制器上的属性,对吧?