This article discusses some guidelines that will make your life easier when migrating an Objective-C project to Swift. The most important thing to remember is that this process is a marathon and not a sprint.
It is always better to try to convert each file individually. However, depending on the situation, it might be better to convert multiple files together.
Converting one class at a time
As mentioned, this is the preferred approach as it almost always leads to the least amount of issues during, and after, conversion. Here's how, and when, you should take this approach:
Start with a file that doesn’t have any dependencies on other Objective-C files in your project. Since there are no dependencies, the converter will have all the information needed to convert this file. This article explains what kind of information the converter needs to produce correct results.
It's clearly unrealistic to expect that all of the files in your project are independent; in fact, it's usually the case that only a small number of utility and helper classes in a project are independent and the rest aren't.
Converting multiple classes at the same time
This approach is preferable when you have interdependent files. For example, take the two classes: FirstClass and SecondClass. Let's assume that each of these classes depends on declarations found in the other one; it would make sense to convert both these files at the same time so that the converter has all the information it needs for a slightly better conversion result.
However, using the Advanced Project Converter to convert one file at a time is still considered the most convenient approach for 95% of cases, just because of the simplicity.
A foo-bar example
Imagine having FirstClass.h with the following property defined in its header:
// FirstClass.h
@property (nonatomic, assign) BOOL myBooleanProperty;
Now consider another SecondClass which uses FirstClass in its implementation as follows:
// SecondClass.m
if (foo.myBooleanProperty) {
// do stuff
}
Converting SecondClass on its own would produce the following wrong result:
// SecondClass.swift
if foo.myBooleanProperty != nil {
// do stuff
}
This is because the converter has no idea that myBooleanProperty actually has type Bool and not just a normal, nullable object. However, feeding both files into the converter at the same time would have produced the correct result.
Rule of Thumb
As a general rule of thumb, try to always convert files with the least number of dependencies and then go from there.
While converting multiple files together can give the converter more information on property types and declarations, the convenience of converting one file at a time often outweighs the slightly better results from converting multiple files together.
Finally, we recommend reading our article on converting a project using Swiftify’s Advanced Project Converter which contains good examples of file dependencies.
Comments
0 comments
Please sign in to leave a comment.