HomeKit completion block in Swift: Cannot convert the expression's type 'Void' to type 'String!'
The parameters for your completionHandler closure are reversed, they should be:
(home: HMHome!, error: NSError!)
Also note that you don't need to specify the types for the parameters, since the method signature specified them for you - thus you can simply list the parameter names you want to use, and they will automatically be assured to be of the correct type, for example:
homeManager.addHomeWithName("My House", completionHandler:{ home, error in if error { NSLog("%@", error) } })
--edit--
Also note that when I wrote 'you can simply list the parameter names you want to use, and they will automatically be assured to be of the correct type', that is to say that they will be typed according to the order in which they are listed - e.g. if you had used error, home in
instead, then those would be your parameter names however the parameter error
would be of type HMHome!
, and home
would be of type NSError!
(since that is the order in which they appear in the parameter list for the closure in the method's signature)
Your arguments are ordered inconsistently; the signature for the completionHandler
in addHomeWithName
is
(HMHome!, NSError!) -> Void
thus use:
homeManager.addHomeWithName ("My House", completionHandler: { (home: HMHome!, error: NSError!) in if error { NSLog("%@", error) }} )
Using a trailing closure and allowing the compiler to infer the types lets you simplify the above to:
homeManager.addHomeWithName ("My House") { if $1 { NSLog ("%@", $1) }}