Coder Social home page Coder Social logo

kennykwok1 / mixed-swift-objc-framework Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danieleggert/mixed-swift-objc-framework

0.0 1.0 0.0 136 KB

Shows how to mix Swift and Objective-C code inside a framework target

Swift 37.09% Objective-C 62.91%

mixed-swift-objc-framework's Introduction

How to Mix Swift and Objective-C in a Framework Target

In an app target, a bridging header does the trick. Inside a framework target, things are trickier.

Out of the box, Swift code can access ObjC code defined in header files which have an #import inside the frameworks umbrella header. These are also the public header files. If we want Swift to access private classes from within the same framework, we have to dance a small dance.

Inside Swift code we can only import so called modules. The trick is to define a module which in turn contains all the ObjC headers that we need the Swift code to access.

The sample project contains an app MyApp which uses a framework called Foo.

Foo

The framework does its usual stuff, and in addition there's a directory called FooPrivate -- this is actually a module. Inside it there's a module.modulemap file which specifies the ObjC header files with relative paths. The framework target for Foo has its SWIFT_INCLUDE_PATHS set to $(SRCROOT)/Foo which is the parent directory of the FooPrivate directory.

With this, Swift code can import FooPrivate and then use (private) ObjC classes.

Private Module

When Swift code accesses the Foo module created by the framework, it needs to find the FooPrivate module, too.

We can work around this issue by adding

module FooPrivate {
    export *
}

to the .modulemap of the framework. In Xcode this can be done by simply setting MODULEMAP_PRIVATE_FILE to point to a module map file with this content.

mixed-swift-objc-framework's People

Contributors

danieleggert avatar

Watchers

 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.