Mixed language framework Mixed language framework objective-c objective-c

Mixed language framework


I believe your problem may be down to the Access Modifiers in your Swift class, however I've written a short guide and sample project to help you:

Sample project can be found here

There are two parts to having a mixed language framework:

  1. Importing Objective-C into Swift
  2. Importing Swift into Objective-C

1. Importing Objective-C into Swift

This is, for example, if you have an Objective-C class named Player that you want to add to a swift class called Game.

According to the documentation, you need to do these two steps to import the Player object into the Game object.

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.

Defines Module

  1. In your umbrella header file, import every Objective-C header you want to expose to Swift.

    #import <Framework/Player.h>

Make sure your Player header file in Objective-C is marked for public target membership in the framework:

Public Header File Setting

Following these steps, it's possible to import the Player Objective-C class into the Game Swift Class:

import UIKitpublic class Game: NSObject {    public let player: Player    public init(player: Player) {        self.player = player        super.init();    }} 

2. Importing Swift into Objective-C

For importing the Swift Game class into the Player object, we can follow a similar procedure.

  1. As before; Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.

Defines Module

  1. Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:

    #import <ProductName/ProductModuleName-Swift.h>

    In my case, this works as:

    #import <SwiftObjC/SwiftObjC-Swift.h>

    and I assume, for you:

    #import <MyKit/MyKit-Swift.h>

    Therefore, make sure that all the properties, methods and classes you want to access are defined as public in your swift file or else they won't be visible to Objective-C .

I have uploaded my sample project showing how this all works on GitHub https://github.com/elliott-minns/SwiftObjCTestFramework

I hope this helps you with your problem.


I think you need to do bridging-header to access your obj-c code in swift.More in the linkhttps://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

so it is a file with your imports from obj-c transfering to swift. names are very important so care about that. hope it helps

p.s. also you need to add this header path to your project settings


From the Documentation at Apple: A Swift class must be a descendant of an Objective-C class to be accessible and usable in Objective-C. For more information about what you can access from Objective-C and how the Swift interface is imported, see Swift Type Compatibility.

So your public swift class needs to extend NSObject for example.