如何使用 Swift 使用多个参数调用 ViewDidLoad 中的函数
How to call a Function in ViewDidLoad with multiple parameters using Swift
我想在 ViewDidLoad 方法中使用多个参数调用一个函数。但它显示错误
Missing argument 'didFindNewAccessory'in call
这是我的代码:
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()
}
})
}
})
}
您必须为函数提供参数 addaccessory
。
在您的情况下,您必须有一个 browser: HMAccessoryBrowser
和 accessory:HMAccessory
,这就是为什么您的调用 addaccessory(self)
因参数无效而出错的原因。
你应该有这样的东西:
addaccessory(yourBrowser, didFindNewAccessory: yourAccessory)
我想在 ViewDidLoad 方法中使用多个参数调用一个函数。但它显示错误
Missing argument 'didFindNewAccessory'in call
这是我的代码:
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()
}
})
}
})
}
您必须为函数提供参数 addaccessory
。
在您的情况下,您必须有一个 browser: HMAccessoryBrowser
和 accessory:HMAccessory
,这就是为什么您的调用 addaccessory(self)
因参数无效而出错的原因。
你应该有这样的东西:
addaccessory(yourBrowser, didFindNewAccessory: yourAccessory)