xcode 无法使用类型为“(字符串)”的参数列表调用 <method>

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

Cannot invoke <method> with and argument list of type '(String)'

xcodeswiftviewcontrollerappdelegate

提问by OrangePot

Im trying to write an OSX app. A functionality of this app is that it displays the machine IP address. The address is fetched when the program is opened (AppDelegate.swift):

我正在尝试编写一个 OSX 应用程序。此应用程序的一个功能是显示机器 IP 地址。程序打开时获取地址(AppDelegate.swift):

@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {

      var ipadd:String = ""
      var path:String = ""
      var status:String = ""


      func applicationDidFinishLaunching(aNotification: NSNotification) {
             ipadd = getIFAddress()  //<-- ip stored in here as String
             println(ipadd)   //successfully prints out the ip
             ViewController.setIpDisp(ipadd)   //error on this line

      }
   ...
 }

And in ViewController.swift:

在 ViewController.swift 中:

class ViewController: NSViewController {

    @IBOutlet weak var ip: NSTextField!
    ...

    func setIpDisp(ipin: String){
         ip.stringValue = ipin 
    }

To be exact, the error is "Cannot invoke 'setIpDisp' with an argument list of type '(String)'

确切地说,错误是“无法使用类型为‘(字符串)’的参数列表调用‘setIpDisp’

Thanks

谢谢

回答by Rob

The AppDelegateis trying to call a ViewControllermethod that is updating an @IBOutletin the view controller's view. It needs a valid ViewControllerinstance to do that.

AppDelegate试图调用一个ViewController正在更新的方法,@IBOutlet在视图控制器的看法。它需要一个有效的ViewController实例来做到这一点。

But this is backwards: The app delegate should not be trying to call view controller methods. The view controller can call methods/properties of the app delegate, but the app delegate really shouldn't be calling view controller methods.

但这是倒退:应用程序委托不应尝试调用视图控制器方法。视图控制器可以调用应用程序委托的方法/属性,但应用程序委托实际上不应该调用视图控制器方法。

If you need to update the IP number field in the view controller, then the view controller should be initiating this (e.g. in viewDidLoad):

如果您需要更新视图控制器中的 IP 号码字段,则视图控制器应该启动此操作(例如在viewDidLoad):

class ViewController: NSViewController {

    @IBOutlet weak var ip: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        updateIpDisp()
    }

    func updateIpDisp() {
        let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

        ip.stringValue = appDelegate.getIFAddress()
    }

}

Or, if you wanted, the AppDelegateset some ipaddstring property in its initmethod (not applicationDidFinishLaunching), and then the updateIpDisp()method could retrieve the property's value from the app delegate, too. (Given that IP numbers are dynamic and can change, that doesn't seem right to me, but do it however you want.) Anyway, that might look like:

或者,如果您愿意,可以在其方法中AppDelegate设置一些ipadd字符串属性init(不是applicationDidFinishLaunching),然后该updateIpDisp()方法也可以从应用程序委托中检索该属性的值。(鉴于 IP 号码是动态的并且可以更改,这对我来说似乎不正确,但可以随心所欲。)无论如何,这可能看起来像:

class AppDelegate: NSObject, NSApplicationDelegate {

    var ipadd: String!

    override init() {
        super.init()

        ipadd = getIFAddress()
    }

}

and

class ViewController: NSViewController {

    @IBOutlet weak var ip: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        updateIpDisp()
    }

    func updateIpDisp() {
        let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

        ip.stringValue = appDelegate.ipadd
    }

}

But the view controller should be requesting the IP number from the app delegate and updating its own view. But the app delegate has no business calling methods in the view controller(s).

但是视图控制器应该从应用程序委托请求 IP 号并更新它自己的视图。但是应用程序委托在视图控制器中没有业务调用方法。

回答by martin meincke

Your function isn't static, so make sure to initialise an instance of it, like so

你的函数不是静态的,所以一定要初始化它的一个实例,就像这样

    @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
      let viewController = ViewController()

      var ipadd:String = ""
      var path:String = ""
      var status:String = ""


      func applicationDidFinishLaunching(aNotification: NSNotification) {
             ipadd = getIFAddress()  //<-- ip stored in here as String
             println(ipadd)   //successfully prints out the ip
             viewController.setIpDisp(ipadd)   //error on this line

      }
   ...
 }