简体   繁体   English

用于Swift和Objective C项目的Swift框架

[英]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. 该项目将被编码为Swift语言,但是使用此框架的客户可以自由使用swift或Objective-C框架。 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. 在快速代码中使用结构,但是无法在目标C框架中使用它。
  • optionals are missing objective c 可选项目缺少目标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. 即使我为Swift和Objective C编写了不同的文件集,我如何将它们映射到同一项目下的不同框架上。
  • Enums with other than Int as rawValue can't be used. 不能使用Int以外的枚举作为rawValue。
  • 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 PS-任何指向教程或博客的链接也将非常有帮助

I did this and got some unexpected results: I have trouble integrating the framework in Swift application. 我这样做了,并且得到了一些意想不到的结果:我无法在Swift应用程序中集成框架。 Objective-C works just fine. Objective-C正常工作。 You mentioned some of the caveats here. 您在这里提到了一些注意事项。

I suggest doing this iteratively while writing test application in Objective-C which uses all the features. 我建议在使用所有功能的Objective-C中编写测试应用程序时,反复进行此操作。 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. 这样,如果某些功能不能很好地跨越Swift到达Objective-C的边界,它将被尽早发现。

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. 您对问题的评论通常是正确的,只有一个小例外:Objective-C不缺少可选项,它们在变量和方法参数上显示为nullable/nonnull修饰符。 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. 在Objective-C中使用Swift ErrorNSError之间的桥梁。 The conversion is not always as smooth at it could be, so better use NSError in exported code. 转换并不总是那么平滑,因此最好在导出的代码中使用NSError

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. 如果在框架中混合使用Objective-C和Swift,则不能使用桥接头,而要使用倾向于变得非常大和复杂的modulemap文件。

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. 由于无法将框架嵌入框架中,因此必须确保应用程序为其目标设置ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES标志。 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. 另外,在为框架创建胖库时,必须从分布式SDK中剥离这些文件。

And, as I said in the beginning, I still have no success using the resulting mixed language framework in Swift application. 而且,正如我在一开始所说的那样,在Swift应用程序中使用生成的混合语言框架仍然没有成功。

Hope, this will add to your list of things to take into account when developing the library. 希望这会添加到您开发库时要考虑的事项列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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