Search code examples
swiftios-homekit

How to call a Function in ViewDidLoad with multiple parameters using Swift


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

Missing argument 'didFindNewAccessory'in call

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()
                    }
                })
            }
        })
    }

Solution

  • 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(yourBrowser, didFindNewAccessory: yourAccessory)