I am having an issue with using the IOS-SDK. I am getting the following error:
Cannot convert value of type ‘([SparkDevice]?, Error?) -> Void’ to expected argument type ‘(([Any]?, Error?) -> Void)?’
I appreciate any and all help, I have been at this for almost a full day now.
My IOS code is:
@IBAction func listDevices(_ sender: Any) {
var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
if let _ = error {
print("Check your internet connectivity")
}
else {
if let d = devices {
for device in d {
if device.name == "myNewPhotonName" {
myPhoton = device
}
}
}
}
}
}
}
Did you look at the example code in the docs? It shows this,
var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (sparkDevices:[AnyObject]?, error:NSError?) -> Void in
if let e = error {
println("Check your internet connectivity")
}
else {
if let devices = sparkDevices as? [SparkDevice] {
for device in devices {
if device.name == "myNewPhotonName" {
myPhoton = device
}
}
}
}
}
I looks like from the error message that you probably need to change AnyObject to Any and NSError to Error (I think those are Swift 3 changes). Also, be sure to change “myNewPhotonName” to the name of your actual device.
Thank you! I was searching forever to find the correction I needed for Swift 3 changes.
A followup question for you: Any ideas on the correcting the following to work on Swift 3 for educational purposes? (It is from an example tutorial on https://github.com/spark/spark-sdk-ios#usage):
SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
It threw the following error:
Cannot convert value of type ‘([SparkDevice]?, Error?) -> Void’ to expected argument type ‘(([Any]?, Error?) -> Void)?’
I am assuming there is a similar Swift 3.0 change that would fix this.
Thanks Ric. It works with your suggestion. I initially missed the fact that “sparkDevices:[SparkDevice]?” should be “sparkDevices:[Any]?” instead.
It would be nice if this was up to date on the docs page.