Merging an Objective-C project with Swift; what is the best method?

I want to port our large iOS project to Objective-C in Swift. So what's the best way: import Swift files into Objective-C or import an Objective-C project into a new new Swift project? Which will be the fastest?

thanks

+5
source share
5 answers

After I spent on the project migration to Swift, I can say that this is not a silver bullet in this matter. It really depends on the thread set in the team you are working on.

From my point of view and experience, you should take an existing project and start writing all the new logic using Swift and at the same time try to rewrite the business lawyers of the application in a modern way. For example, the network layer, the database layer. This way you reduce application regression and get a controlled migration flow.

Pros:

  • The new logic will be in Swift.
  • Incremental migration regression will be balanced.
  • Support for legacy code that cannot be rewritten in Swift
  • Writing code using the Swift method (protocols, functions, generics, structures)

vs

  • Time
  • Application regression
  • Rather large rewriting of code.
+3
source

If you are starting a new project with the goal of using Swift in the future, I would create a new Swift project and then contact your Objective-C framework. This is what we did where I work, and slowly over time we remove everything that is not used from the Objective-C project. It may not be faster than importing everything into the same project, but it will give your code some separation from the old Objective-C code.

+1
source

This is a very complex question, and the answer is: Depends ... I think there are a few questions that need to be completed before the answer:

  • Your project is "open", your team is developing new functions on the same code base? or has a solid and reliable code base and few changes?
  • Can your company provide a "NEW VERSION", fully created in Swift, with new technology?

Basically, a lot of refactoring (like language migration) involves rebuilding work, most things will not work if you just decrypt the code base, and if you do, you lose all the benefits of Swift, and there is no Win to change the code .

In my opinion, creating a new project is just a good thing if you make a new version of your application. Cleaner, better, but, as I said, there will be a lot of work and reorganization.

Otherwise, just paste the Swift code into the Objective-C application, this will cause a lot of trouble and confusion. But it’s faster, and you can continue to work with new features and implement improvements in your application.

+1
source

So, here are my 2 cents for your problem: I would recommend slow code conversion or, rather, refactoring for Swift, if required for new parts of the code. In addition, if the converted code is currently used in other side projects (function branches, etc.), you cannot guarantee that it will work anyway.

I hope you got unit tests in place because they could make life easier. In any case, consider the aspect of regression testing your application functions. The libraries you use should now be frameworks (some libraries may not compile as Framework, keep this in mind).

IF you really want to convert the code base, I would do it chirally.

  • Consider the entire Application Layer and within this layer specific classes.

  • Start writing tests for these classes (you can do it in Swift too), Unit Test, integration test, user interface tests

  • Convert a class to a fast class (remember that some logic may change, and other functions will also be affected)

  • Implement a bridge header (if necessary) that you can use as the Application Layer interface for other interfaces - this will even improve your understanding of how different parts of the code play together. If you notice any strange statements. Publicity, too many random accesses for different classes or something else. Do not touch it. Actually note that down

  • Do extensive tests of your little change

  • Start over with the next class.

  • If all classes have finished at your current application level and you haven’t broken anything. Great job. Now take a look at your notes.

  • Continue from the next layer.

In short: I would only convert these parts to Swift, which in any case must be changed (refactoring, new functions, errors, whatever). If the code worked, and you see no other reason than “okay, now it's Swift”, I would take into account only the conversion, if I really had time for this, otherwise you could break the working code or even which was installed , which in some cases is different for Swift and Objective-C.

If you really feel that you are the person for this: I would start writing tests ;-) Unit-, Integration and UI Tests. This will help you with your CURRENT code base, but also with your future code base, whether in Objective-C or Swift. And this is a great help for converting code to Swift.

+1
source

I think the best way is to import the Objective-C project into a new Swift. Because you can use target c for a quick converter . This saves thousands of hours of work.

0
source

Source: https://habr.com/ru/post/1266915/


All Articles