Build a library for Swift and Objective-C

安稳与你 提交于 2019-12-08 04:52:47

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/26417735/build-a-library-for-swift-and-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!