简体   繁体   English

Swift vs Objective-C - 用于重用C ++代码

[英]Swift vs Objective-C - for reusing C++ code

We have an android application having the critical code written in C++ in JNI library. 我们有一个Android应用程序,其中包含在JNI库中用C ++编写的关键代码。

We are about to implement iOS version of the same application. 我们即将实现相同应用程序的iOS版本。 The plan is to implement in Objective-c as it's straight forward to integrate C code with it. 计划是在Objective-c中实现,因为它直接将C代码与它集成。 However, as Swift is picking up, we would like to have suggestion on Swift vs Objective-C keeping in mind that we have to use the existing C++ libraries and any Swift bottlenecks. 但是,随着Swift的兴起,我们希望对Swift vs Objective-C提出建议,记住我们必须使用现有的C ++库和任何Swift瓶颈。

The way to reuse C++ code is to write an Objective-C wrapper class, with the header file containing nothing that is C++, but the implementation file written in Objective-C++ (.mm suffix). 重用C ++代码的方法是编写一个Objective-C包装类,其头文件不包含任何C ++,但实现文件是用Objective-C ++(.mm后缀)编写的。

An Objective-C class can be used from Swift and from Objective-C, so for a Swift project, it doesn't matter if you have a few Objective-C classes. 可以从Swift和Objective-C中使用Objective-C类,因此对于Swift项目,如果您有几个Objective-C类则无关紧要。

There is no way currently to call C++ from Swift directly, and I wouldn't expect it for a while. 目前无法直接从Swift调用C ++,我暂时不会期待它。 And if you look at how you call C from Swift, you'll probably decide that you are quite happy with the situation. 如果你看看你如何从Swift调用C,你可能会认为你对这种情况非常满意。

There is no problem writing a Swift application and integrate in it a c++ source code. 编写Swift应用程序并在其中集成c ++源代码没有问题。

Here are the steps: 以下是步骤:

1) Add the c++ source files to your Xcode project. 1)将c ++源文件添加到Xcode项目中。 If you are using a 3rd party libreries you will need to add it as framework to your project. 如果您使用的是第三方libreries,则需要将其作为框架添加到项目中。

2) create a Header bridge file and add it to you project. 2)创建一个Header桥文件并将其添加到您的项目中。 This file will have the c++ functions signature you will like to use from the swift modules. 该文件将具有您希望从swift模块中使用的c ++函数签名。 as explained in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html中所述

3) When you call the c++ functions mention in the Header Bridge file you need to create a special pointers object, as described in - https://gist.github.com/neilpa/b430d148d1c5f4ae5ddd 3)当您在Header Bridge文件中调用c ++函数时,需要创建一个特殊的指针对象,如下所述 - https://gist.github.com/neilpa/b430d148d1c5f4ae5ddd

Hope this will help. 希望这会有所帮助。

Thanks for responding. 谢谢你的回复。 After careful consideration, we decided to go with Objective-C and even released the application. 仔细考虑后,我们决定使用Objective-C,甚至发布了应用程序。

One of the key reason was that most developers in our company are proficient in C/C++ and Objective-c was easy to get comfortable with than the Swift. 其中一个关键原因是我们公司的大多数开发人员都精通C / C ++,而Objective-c比Swift更容易让人感到舒服。

Integration with existing C/C++ code was easy without any bridging code once we defined the Makefile templates and used them within existing Makefiles. 一旦我们定义了Makefile模板并在现有的Makefile中使用它们,就可以轻松地与现有的C / C ++代码集成而无需任何桥接代码。 Only additional thing was to make scripts for fat libraries. 唯一的另一件事是为胖库创建脚本。

Hence based on our experience, I will recommend Objective-C over Swift. 因此,根据我们的经验,我将推荐Objective-C over Swift。

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

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