Phillips HUE SDK in Swift

2019-07-16 14:30发布

问题:


I am very new to Swift! I do have some experience with Objective-C (although it has been around two years since i've really written anything in it). I am trying to use the Phillips HUE SDK with Swift, and am having some trouble! I am trying to re-write the following Objective-C code into Swift:

// Start search for bridges
[self.bridgeSearch startSearchWithCompletionHandler:^(NSDictionary *bridgesFound) {
   //Search complete
   [self showBridgesFound:bridgesFound];
}

I have had no luck looking online for a solution on this, and there is no documentation through Phillips. Has anyone had experience with this, could you please provide assistance?

回答1:

The tricky piece is figuring out the syntax of the Swift equivalent of the NSDictionary.

self.bridgeSearch.startSearchWithCompletionHandler { (bridgesFound: [NSObject : AnyObject]!) -> Void in
    self.showBridgesFound(bridgesFound)
}


回答2:

self.bridgeSearch!.startSearch { (bridgesFound: [AnyHashable : Any]!) -> Void in
            //self.showBridgesFound(bridgesFound)
            print(bridgesFound)
        }

I think you need to use [AnyHashable : Any] in swift 3 as per this answer.