Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to retrieve the list of your application’s tiles already on the Microsoft Band 1

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!

like image 641
Coty Embry Avatar asked Dec 15 '25 19:12

Coty Embry


1 Answers

Your error handling seems to be correct in errorFunction. Just modify the print statement to also print the error object to see what the actual error is.

print("error was not nil, meaning an error occurred... :( \(error)")

You could further look at the error.code and add logic in your app to handle it. MSBErrorTypes.h has a list of possible error code and most likely your code will be in the 300 range.

like image 169
Manjit Riat Avatar answered Dec 17 '25 09:12

Manjit Riat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!