Mixed language system

I have a framework (let it be called MyKit) written in Objective-C, which I distribute with some Swift classes. I am trying to get around this using this documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77

As far as I understand, I do not have the intended class of headers, and instead it puts all of my umbrellas included in the header file (which I understand).

So, I write in MyKit.h:

#import <MyKit/ModelObjectA.h> #import <MyKit/ModelObjectB.h> #import <MyKit/ControllerObjectC.h> 

I do not list ControllerObjectD.swift, although it is also included here. Or should I turn on

 #import <MyKit/ControllerObjectD-Swift.h> 

?

ControllerObjectD uses ModelObjectA and ModelObjectB. Now that I do not have a bridge header file, I get compilation errors because it cannot find these objects.

The documentation says: "Swift will see every headline that you publish publicly in your umbrella headline." and this is true when I import the framework into other projects, but the framework project cannot compile because it does not see it. I turned on the "Define Modules" build setting.

Perhaps something I misunderstood about the title of the umbrella? Where can I say "hello project, this is the umbrella header file"? Compiling the structure if I set the umbrella header file as the header bridge, but it looks like I'm back to the beginning of this path?

Greetings

Nick

+8
ios objective-c frameworks swift
source share
3 answers

I believe that your problem may not be available to access modifiers in your Swift class, however I wrote a short reference and sample project to help you:

An example project can be found here.

There are two parts that have a mixed language structure:

  • Import Objective-C into Swift
  • Import Swift in Objective-C

1. Import Objective-C into Swift

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

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

  • In the Assembly Options section of the Packaging section, verify that the Define Module option for this target framework environment is set to Yes.

Defines module

  1. In the header of your header file, import each Objective-C header that you want to open in Swift.

     #import <Framework/Player.h> 

Ensure that the Player header in Objective-C is marked for public target membership under:

Public Header File Setting

After these steps, you can import the Player Objective-C class into the Game Swift class:

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

2. Import Swift into Objective-C

To import the Swift Game class into a Player object, we can perform a similar procedure.

  • As before; In the Assembly Options section of the Packaging section, verify that the Define Module option for this target environment is set to Yes.

Defines module

  1. Import Swift code from this target environment into any Objective-Cm file within this target environment using this syntax and substituting the appropriate names:

     #import <ProductName/ProductModuleName-Swift.h> 

    In my case, it works like:

     #import <SwiftObjC/SwiftObjC-Swift.h> 

    and I suppose for you:

     #import <MyKit/MyKit-Swift.h> 

    Therefore, make sure that all the properties, methods, and classes that you want to access are defined as public in your fast file, otherwise they will not be visible in Objective-C.

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

Hope this helps you solve your problem.

+25
source share

I think you need to make a header to access your obj-c code in swift. More at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

so this is a file with your import from obj-c translating to fast. names are very important, so take care of this. hope this helps

ps also you need to add this path to your project settings

+1
source share

From the documentation at Apple: The Swift class must be a descendant of the Objective-C class, which must be accessible and used in Objective-C. For more information about what you can get with Objective-C and how to import the Swift interface, see Swift Type Compatibility.

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

0
source share

All Articles