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

时间:2016-05-07 10:58:38

标签: swift ios-homekit

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

  

在调用中缺少参数'didFindNewAccessory'

这是我的代码:

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 个答案:

答案 0 :(得分:1)

您必须为函数addaccessory提供参数。 在您的情况下,您必须拥有browser: HMAccessoryBrowseraccessory:HMAccessory,这就是您的无效参数调用addaccessory(self)错误的原因。 你应该有这样的东西:

addaccessory(yourBrowser, didFindNewAccessory: yourAccessory)