Build a library for Swift and Objective-C

2019-05-31 18:59发布

I need to make a library with swift that support both swift and object c project (also support xcode 5), this will be a distribution library (do not share source code). There are two ways to do this: create a Cocoa Touch Framework or create a Cocoa Touch Static Library. I make some research, but still find out the solution. It seems the Cocoa Touch Framework not support to buid a distribution library, and I'm not sure the Cocoa Touch Static Library work perfect with with Swift yet or not. Any solutions for it. Thanks.

2条回答
时光不老,我们不散
2楼-- · 2019-05-31 19:16

I faced the same issue as you to build one Pod library for both Swift and Objective-C, using pure code respectively without messing languages or include bridging headers. Actually I didn't try it by myself, but I came to several possible solutions:

1) To use a different branches of the same repo:

pod 'YourPodLib', :git => 'https://github.com/user_name/YourPodLib.git', :branch => 'objc'

pod 'YourPodLib', :git => 'https://github.com/user_name/YourPodLib.git', :branch => 'swift'

2) Experimenting with Subspecs as a way to install a subset of your library. (official cocoapods documentation: subspec )

It probably would look like: pod 'YourPodLib/Swift'

Maybe my approach is far away from being ideal, but I would be appreciate if you correct me or suggest a better idea.

查看更多
叛逆
3楼-- · 2019-05-31 19:19

A very good example for this kind of library is Realm.

They both support swift and objective-c project integration. Check the repo on GitHub , and browse it : here

In fact, they have all the library coded in Objective-C, and the add Support for swift specifecity with extensions on objects used by the client. (For example, check the Realm/RLMSwiftSupport.m in their repo)

EDIT

To access a Swift class in Objective-C, just add @objc(MyClass) in your Swift Class and it will be available in Objective-C files.

Read the this for more informations about Objective-C and Swift working together

查看更多
登录 后发表回答