如何使用Swift使用多个参数在ViewDidLoad中调用Function

时间:2022-07-28 18:00:41

I want to call a function in ViewDidLoad method with multiple parameters. But it shows the error

我想在具有多个参数的ViewDidLoad方法中调用一个函数。但它显示错误

Missing argument 'didFindNewAccessory'in call

在调用中缺少参数'didFindNewAccessory'

Here's my code:

这是我的代码:

override func viewDidLoad() {
        addaccessory(self)
}

func addaccessory(browser: HMAccessoryBrowser, didFindNewAccessory accessory:HMAccessory){

        print("Found new accessory")
        home?.addAccessory(accessory, completionHandler: {
            error in
            if error != nil {

                print("Failed to add it to home")

            }else{
                print("Successfully Added")
                self.home?.assignAccessory(accessory, toRoom: self.room!, completionHandler: {
                    error in

                    if error != nil{
                        print("Unable to add to room")
                    }else{
                        print("Successfuly added to ropom")
                        self.findOrCreatedServioceGroup()
                    }
                })
            }
        })
    }

1 个解决方案

#1


1  

You must provide the argument to your function addaccessory. In your case you must have a browser: HMAccessoryBrowser and accessory:HMAccessory, that's why your call addaccessory(self) is wrong with invalid argument. You should have something like this :

您必须为函数addaccessory提供参数。在你的情况下,你必须有一个浏览器:HMAccessoryBrowser和附件:HMAccessory,这就是为什么你的调用addaccessory(self)错误的无效参数。你应该有这样的事情:

addaccessory(yourBrowser, didFindNewAccessory: yourAccessory)

#1


1  

You must provide the argument to your function addaccessory. In your case you must have a browser: HMAccessoryBrowser and accessory:HMAccessory, that's why your call addaccessory(self) is wrong with invalid argument. You should have something like this :

您必须为函数addaccessory提供参数。在你的情况下,你必须有一个浏览器:HMAccessoryBrowser和附件:HMAccessory,这就是为什么你的调用addaccessory(self)错误的无效参数。你应该有这样的事情:

addaccessory(yourBrowser, didFindNewAccessory: yourAccessory)