Git Product home page Git Product logo

swift-objective-c-mixed-project's Introduction

Swift 2.0 and Objective C in a Mixed Project

Example code on how to use Objective C and Swift components together in a project. Use cases:

  • Invoking Objective C classes from Swift
    • In the same project
    • Unit testing Objective C classes from Swift
  • Invoking Swift classes from Objective C
    • In the same project
    • Unit testing Swift classes from Objective C

Why

Since the announcement of Swift it has been possible to use Objective C and Swift code together in the same project. However even with XCode 7, using the two languages together is not very straightforward and the Apple documentation doesn't provide a working example to demonstrate this usage.

This sample makes it easy to get started. It also serves as a guide on using Swift 2.0 and Objective C together in applications for XCode 7.0 upwards.

Invoking Objective C code from Swift

  • Add the Objective C header file to the Bridging header
  • E.g. in the MixedLanguageExample project e.g. to use the ComponentObjectiveC class from any Swift file in the project
    • In MixedLanguageExample/MixedLanugageExample-Bridging-Header.h add
    #import "ComponentObjectiveC.h"
    
    • After this ComponentObjectiveC can be used in any Swift file within the MixedLanguageExample project (e.g. as used in LabelSwift.swift)

Unit testing Objective C classes from Swift

  • Import Objective C classes to test via the test target's bridging header.
  • E.g. to test the ComponentObjectiveC Objective C class from ComponentObjectiveCFromSwiftTests.swift:
    • In the unit test file MixedLanugageExampleTests / MixedLanugageExampleTests-Bridging-Header.h
    • import the Objective C header file
    #import "ComponentObjectiveC.h"
    

Invoking Swift code from Objective C

  • Import the XCode generated header file to use it in the .m file
  • E.g. in the MixedLanguageExample to use the ComponentSwift class from LabelObjectiveC:
    • Make sure for the main target, under Build Settings --> Defines Module is set to Yes
    • In LabelObjectiveC.m add
    #import "MixedLanugageExample-Swift.h"
    

Unit Testing Swift classes from Objective C

  • Make sure for the test target (in the example, MixedLanugageExampleTests), under Build Settings --> Defines Module is set to Yes
  • Import the XCode generated header file in unit tests:
    • E.g. in ComponentSwiftFromObjectiveCTests.m
    • Import the XCode genrated header file for the test target by adding
    #import "MixedLanugageExampleTests-Swift.h"
    

FAQ

  • Q: What about working with Swift 1.0 using XCode 6.xx?

    A: The steps are exactly the same. I created the sample using XCode 7.0 beta as it has better Swift support (even if the beta is unstable)

  • Q: When testing Objective C classes using Swift, do I need to re-import the Objective C header files in the bridging header, in my test project? I've already done this in my production project.

    A: Seems like this is the only way to do it. Think of the bridging header as the new prefix header for Swift to work with Objective C.

swift-objective-c-mixed-project's People

Contributors

gergelyorosz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.