2


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?

5T4TiC
  • 191
  • 2
  • 2
  • 6
  • Translating from ObjC to Swift is really easy. What have you got so far? You're probably almost there. Show your translation and let's fix it. – matt Dec 19 '14 at 21:32
  • @matt I will have to get the exact code I attempted off of my work laptop on Monday. Also, i'm not sure why this was down voted - seems like a legitimate question. – 5T4TiC Dec 20 '14 at 23:48
  • I wouldn't exactly call throwing some Objective-C code at the wall and asking SO to translate it into Swift for you "legitimate", no. – matt Dec 20 '14 at 23:57
  • 2
    Isn't the purpose of SO to help fellow developers that have questions or issues? Maybe this isn't a very complex question but for someone very new at swift and relatively inexperienced with Objective-C, should I have just sat in confusion? I spent a good amount of time trying to find the solution online and learning about the problem but was stuck. – 5T4TiC Dec 22 '14 at 01:53
  • No one is suggesting you should just sit in confusion. Remember my first comment? "Translating from ObjC to Swift is really easy. What have you got so far? You're probably almost there. Show your translation and let's fix it." I was helping you to turn this into a great SO question, and then I would have been happy to help work with you to solve it. Since then your response has been to do nothing but complain and you still haven't posted your attempted translation. – matt Dec 22 '14 at 02:11
  • I made one comment about the question being down voted, I also mentioned that I don't have the code in front of me and won't until Monday. I do appreciate that you are willing to help work through the code with me, and I still plan on posting the code Monday. I was just confused as to why this was originally down voted, but thanks for clearing it up. – 5T4TiC Dec 22 '14 at 02:14
  • I don't know why it was downvoted. It's a "wisdom of the crowd" site. You have to take the bitter with the sweet. But I've been here a while, so I do have a sense of what makes a good SO question, and of what the crowd might downvote for. The way your question was asked, it seemed just plain lazy: "I can't do this, please just write my code for me." That is not in the SO spirit. Showing your own attempt, and giving details as to what's going wrong, is at least an effort that will gain you some goodwill. Your question showed _no_ effort - and that's exactly what people downvote for. – matt Dec 22 '14 at 02:17
  • Fair enough, I'm still new to SO and will definitely keep that tip in mind for future questions! Thanks – 5T4TiC Dec 22 '14 at 02:22
  • I had something like this (altered it a few times but couldn't get any of them to work obviously): bridgeSearch.startSearchWithCompletionHandler(bridgesFound: Dictionary) – 5T4TiC Dec 22 '14 at 14:46
  • That would be miles off. Even if there were a pure Dictionary type, this parameter needs to be typed as a _function_, not as a dictionary. – matt Dec 22 '14 at 15:50
  • Could you briefly explain the meaning behind the answer given below (specifically `{ (bridgesFound: [NSObject : AnyObject]!) -> Void in showBridgesFound(bridgesFound)`) I sort of understand that it is calling the startSearch... function, and storing the data (i think) into the bridgesFound variable. What I am unsure of is what `[NSObject : AnyObject]!) -> Void in` represents – 5T4TiC Dec 23 '14 at 13:13
  • I already sort of answered that in my preceding comment. The parameter is a function (that takes a dictionary) - not a dictionary. If you need a longer explanation, please ask a new question (and blip me through a comment)! I'd love to explain this at length. – matt Dec 23 '14 at 18:23

2 Answers2

4

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)
}
picciano
  • 22,341
  • 9
  • 69
  • 82
  • Ah, I will have to try this (when I get back to my work computer on Monday). I had something very very similar but had errors, hopefully this is all i need - thank you :) – 5T4TiC Dec 20 '14 at 23:48
  • @5T4TiC: Do you have sample code for Philips HUE swift integration? – sabir Mar 21 '17 at 12:00
0
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.

kernelpanic
  • 530
  • 2
  • 8
  • 26