简体   繁体   中英

Swift framework for Swift and Objective C projects

I am building a new framework. The project is to be coded into Swift language, however the clients using this framework have the freedom of using either swift or Objective-C framework. The question is how do I start. There could be numerous issues like

  • using structs in swift code but it cannot be made available in objective C framework.
  • optionals are missing objective c
  • Even if I write different set of files for Swift and Objective C, how will I map them onto different frameworks under the same project.
  • Enums with other than Int as rawValue can't be used.
  • Tuples would not work

I know there have been a few questions around this but none have any satisfactory answer.

PS - Any link to a tutorial or blog would be super helpful too

I did this and got some unexpected results: I have trouble integrating the framework in Swift application. Objective-C works just fine. You mentioned some of the caveats here.

I suggest doing this iteratively while writing test application in Objective-C which uses all the features. This way if there is some feature that does not cross Swift to Objective-C boundary well, it will be discovered as early as possible.

Your remarks about issues are generally correct with one small exception: optionals are not missing from Objective-C, they appear as nullable/nonnull modifiers on variables and method parameters. Although this does not replace optionals fully, it helps detecting issues early in the process.

Here is a random list of some other issues I discovered:

Bridging between Swift Error and NSError used in Objective-C. The conversion is not always as smooth at it could be, so better use NSError in exported code.

If you mix Objective-C and Swift in your framework, you cannot use bridging header, instead using modulemap files which tend to turn pretty large and complex.

Since you cannot embed frameworks inside a framework, you have to make sure that the application sets ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES flag for its target. This has to be clearly indicated in the documentation. Also, when creating fat library for your framework, you have to strip these files from the distributed SDK.

And, as I said in the beginning, I still have no success using the resulting mixed language framework in Swift application.

Hope, this will add to your list of things to take into account when developing the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM