Clms.Vie
Clms.Vie

Reputation: 11

Using Philips-Hue Obj-C in Swift Project

I am a complete beginner when it comes to coding (except for web) but wanted to start with it. So I bought a philips hue set which I wanted to control via Swift2. I started a online Swift2 course which helped me to get started (still beginner) and so I hoped HUE will help me to learn coding Swift2 in a playful way.

But: The API provided by Philips is in Obj-C which absolutely confuses me. There should be a way to integrate it into a Swift project but as I said.. as a beginner I can't manage to do it (http://www.developers.meethue.com/documentation/apple-sdk).

After downloading the example I was able to run it on my lights which worked fine, but I am not able to understand nor change the code. I googled and tried to find an example where someone included it into Swift so that I can start playing with the lights and start using what I learned so far.

Because there is so little information out there I came here, hoping you guys could help me getting started - how would a project look like, so that I can use the obj-C parts (especially connecting to the bridge) in my swift project? Is there somewhere a demo out there I wasn't able to find? Sorry for my "unprofessional" question but it would mean a lot to me to get started with this :)

Thanks so much!

Upvotes: 0

Views: 319

Answers (1)

Alex Pelletier
Alex Pelletier

Reputation: 5123

Swift and Obj-C are interchangeable, as apple says:

Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project. This natural workflow makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language. Reference


To create a header bridge file follow these steps

  1. Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
  2. Name your file “YourProjectName-Bridging-Header.h”. Example: In my app Station, the file is named “Station-Bridging-Header”.
  3. Create the file.
  4. Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
  5. Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.
  6. Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes. Reference

Once you have created your bridge file and hooked it up, you can place your Obj-C import statements in there (#import <HueSDK_iOS/HueSDK.h>) and then use the Obj-C functions in swift.

If their docs are bad I would suggest doing a really quick intro to Obj-C, just so that you can easily translate the sample project to your new project.

Upvotes: 1

Related Questions