Search code examples
iosobjective-cswiftmacosphilips-hue

Trying to figure out CompletionHandler in Swift?


So I'm trying to do some programming with Phillips HUE lights and I've already made a bridging header and imported the necessary info to use the Hue SDK with Swift, but all of the guidance is written for Objective-C. I'm having trouble understanding/declaring this function in Swift:

// Start search for bridges
[self.bridgeSearch startSearchWithCompletionHandler:^(NSDictionary *bridgesFound) {

    // Search is complete, handle results (dictionary contains IP and mac addresses of bridges found)

    [self showBridgesFound:bridgesFound];
}

When I go to call this in Swift, I type bridgeSearch.startSearch() and Xcode automatically adds this with a Completion handler

//Search for bridges
let bridgeSearch: PHBridgeSearching = PHBridgeSearching()        
bridgeSearch.startSearch { ([AnyHashable : Any]?) in
        //code
}

I know how to get the information (found IP addresses) stored into an NSDictionary in Obj-C, but I'm not sure what to do here in terms of storing the IP addresses into some sort of data structure.

Thanks!


Solution

  • The problem is merely that you have not given the incoming parameter any name. Give it one:

    bridgeSearch.startSearch { bridgesFound in
        // code involving bridgesFound
    }