Im creating a Microsoft Band 1 application for iOS with Swift This function on the documentation has me going cross eyed. Please help.
I know functions can act as types in Swift i.e.
var exampleFunction: (String, Int) -> String
is a function that takes two parameters, a string and an int and returns a string.
The method I'm looking at says the following in Xcode (Swift language):
tilesWithCompletionHandler(completionHandler: (([AnyObject]!, NSError!) -> Void)!
which I believe is saying, titlesWithCompletionHandler
takes in a parameter which is a function of type [AnyObject]!, NSError!) -> Void
I'm not sure about the ()!
surrounding the whole thing though I know this is forcing a value out of the optional.. thats hard to understand as well.
on the website for the documentation it is written in Objective-c which shows this as the method definition:
[self.client.tileManager tilesWithCompletionHandler:^(NSArray *tiles, NSError *error) {
if (error){
// handle error
}}];
what I have attempted is to construct a function that is the type this is asking for:
//I had to create this function to match the parameter that the tilesWithCompletionHandler method required
func errorFunction(tileArray: [AnyObject]!, error: NSError!) -> Void {
print("hello")
if((error) != nil) {
//handle error
print("error was not nil, meaning an error occurred... :(")
}
else {
print("i got here")
self.tileArray = tileArray
}
}
then I created a type and assigned it to this function like so (which fixed the errors Xcode was griping about when I called the method Im trying to use):
let customFunction: (([AnyObject]!, NSError!) -> Void)! = errorFunction
the ()!
part around the type still confuses me though
finally I call the function that I'm needing to call to get the tiles and pass in the function I just constructed
myBand.tileManager.tilesWithCompletionHandler( customFunction )
Edit: the error was not related to the problem. The print statements do print now, but I get into the error flow.
Am I going about this the right way?
Also, I'm trying to figure out how to handle the error part of the parameters. Do I need to use a
do {
try //some code I need to figure out what to write
} catch let error as NSError {
//code to handle error
}
There's just a lot going on in this method call for me to fully grasp. Any help would be much appreciated. Thank you for your time!
After Larme's comment I was able to get it working with a closure in Swift. I'm curious if the method I was using in my question would of worked...
This is what I did after updating my print statement that was suggested as well which let me learn you can print errors this way too! :
myBand.tileManager.tilesWithCompletionHandler( {(tiles:[AnyObject]!, error: NSError!) -> Void in
if((error) != nil) {
//handle error
print("Error in .tilesWithCompletionHandler: \(error)")
}
})
It's just a closure which apparently is equivalent to a block in Objective-c which I didn't really know about before now (the block part that is).
Thanks for the help everyone!