Over the past few years I have received a number of questions with regards to Swift & Objective-C. Specifically related to the future of the 2. I will try to address those questions in the form of an FAQ.
Should I learn Swift or Objective-C?
This is a question that I get from developers new to iOS/macOS App Development. Ideally speaking, you should learn Swift. As that is going to become the main language for App development on Apple’s ecosystem. However, the reality is a little different. There are a large number of applications that are written in Objective-C. You are likely to encounter them at your workplace. You may also have to maintain, upgrade & improve those apps. In such a case, it makes sense to learn Objective-C too.
Can I mix Swift & Objective-C in the same project?
Yes! But remember that you should check for feature compatibility between the 2 languages. Adding Swift code to an Objective-C project may not be very beneficial as only those features that are compatible with Objective-C can be written in Swift.
Going the other way round is not a problem. You can read more about that here:Mixing Swift & Objective-C
Will Objective-C be deprecated in the future?
That is an interesting question. There is no formal announcement from Apple stating the Objective-C is going to be deprecated. However, one can expect more attention to be paid to Swift. That is where most of the newest techniques, tools & technologies are going to be available. Objective-C will keep running as it is as of now.
Can I mix Swift with other Programming Languages?
Swift can easily be mixed with Objective-C. If you wish to incorporate C++ or C code in your Swift Project then wrapping them in Objective-C code allows you to achieve this.
Apart from that Swift does support working with C code code. You can read about that here:Interacting with C APIs.
Swift does not provide interoperability support for any other languages as of now.
Which version of Swift should I use?
It is recommended that you use the latest available version of Swift. However, the actual version that you work on depends on many other factors like: compatibility with OS Versions, support & business related choices.
Why shouldn’t we just convert all our Objective-C code to Swift and keep things simple?
A very tempting proposition. However, practical realities prevent us from doing this. The process of converting from Objective-C to Swift is time consuming. Apart from having to convert the syntax, the code also needs to be optimised taking into account the new features that are available. This will mean extensive testing and quality assurance. Most companies will not invest their resources into this endeavour.
A better approach is to migrate to Swift gradually. Here are some ways to do this:
- If its a brand new product/app that you are creating, start it in Swift.
- Any new reusable code components that are being created should be done in Swift (they should be Objective-C compatible if you intend to use this code in Objective-C projects).
- If any part of a product is going to undergo heavy change, either due to a bug fix or a new feature. This is a good time to convert it into Swift.
A good example is how Apple is approaching the process of migrating to Swift. They are doing it component by component.
I have been developing apps in Objective-C for some time. I am able to create any reasonably complicated app now. If Objective-C hasn’t been deprecated then should I start making apps in Swift?
This is a choice that you have to make. It is recommended that new apps (at the very least) be made in Swift as that is the language that will undergo the maximum amount of changes & improvements in the future.
What do you suggest as a trainer?
Another question that I get very often. It depends on the situation. I would say learn both Swift & Objective-C. You can skip learning Objective-C if you are confident that you will not have to work with any projects written in that language.
If I am starting on a brand new project I would use Swift. But if its an Objective-C project I would stick to Objective-C.
Can Swift development only be done on macOS?
No! Swift development can also be done on Linux. However, iOS/macOS/tvOS/watchOS App Development can only be done on macOS through Xcode.
How should I migrate to Swift?
There are different approaches that one can use. It all depends on the situation and needs of your organisation. Here are some things that you can do:
- Start development of brand new apps (from scratch) in Swift.
- If you are creating a brand new library which will be used for future projects then go ahead with Swift.
- If a major component of an existing app is going to be changed significantly then you can go ahead with Swift.
You can do all or some of the above. There may be other strategies too. You should also factor in the cost of migration from one language to another.
One thought on “When to use Swift & when to use Objective-C?”
Pingback: Migrating to Swift from Objective-C | Arun Patwardhan's Blog