iosobjective-cxcodeswiftcocoapods

How to integrate Cocoapods with a Swift project?


As Apple introduced Swift, their new programming language, I wonder how you can integrate it with existing Objective-C libraries that are available via CocoaPods?


Solution

  • It seems that the process is similar to the one described in Mix and Match section of Using Swift with Cocoa and Objective-C documentation.

    1. Create your Podfile and run pod install.
    2. Create a new Objective-C header file, Example-Bridging-Header.h, and add it to the project.
    3. Add import statement to the bridge header.
    4. Set Objective-C Bridging Header for your target:

    enter image description here

    Now you can use your library, in that case, MKUnits, in your Swift file:

    let kilograms = NSNumber.mass_kilogram(2)()
    let pounds = NSNumber.mass_pound(10)()
    let result = kilograms.add(pounds)
    println(result)
    

    More here: Integrating Cocoapods with a Swift project