Since its introduction over 2 years ago, Swift gathered a lot of feedback, both positive and negative, and quickly established itself as a popular new iOS app programming language, as well as a great tool to write software for various Apple products. There are a lot of reviews on the Internet comparing Swift vs Objective-C and trying to determine the pros and cons of each language.
At Apriorit we have extensive experience of developing software for both iOS and Mac OS, and this article compares two languages together and covers Objective-C vs Swift differences in order to help you choose Mac OS X and iOS programming language for your project.
What is Objective-C
Originally created in the early 80s, Objective-C first gained its popularity as the main language for NEXTSTEP operating system. When NEXTSTEP creator NeXT was merged with Apple in 1996, NEXSTEP became the basis of a new Apple OS, solidifying Objective-C as the main language for Apple systems for years to come.
For a long time Objective-C was a sole primary programming language used for creating OSX and iOS applications. At its core Objective-C is a superset of regular C with added object-oriented features and dynamic runtime.
Objective-C++ is a variation of Objective-C that adds support for C++ syntax, allowing to compile source files containing both Objective-C and C++.
What is Swift
A new Swift programming language from Apple can be best described as Objective-C without the C. It was first introduced by Apple at WWDC 2014. It is a Mac OS and iOS coding language that also supports tvOS and watchOS.
Swift combines strong suits of both C and Objective-C, without having direct built-in C compatibility and all the constrains that go with it. Modern feature set and safe programming patterns are core advantages of Swift over Objective-C, allowing it to provide flexibility and allow for a quick and easy development. With the support of popular mature frameworks, such as Cocoa and Cocoa Touch, Swift completely revamps our understanding of how to make software for Apple products.
Apple themselves consider Swift to be an interactive and expressive language, that is very intuitive and fun to work with. Swift code is inherently safe, yet allows to produce a very fast software.
A little bit of statistics
Apple does not provide public statistics, and there is no a single comprehensive Objective-C vs Swift comparison regarding their popularity and usage online. However, based on various information resources (such as GitHub, StackOverflow), it can be concluded that the popularity of Swift continues to grow.
According to data provided by PYPL (http://pypl.github.io/PYPL.html), Swift caught up in popularity to Objective-C in just a year.
Diagram below shows the popularity of famous languages according to PYPL index (2004 - 2016).
Alternative statistics – TIOBE index
Netherland-based company TIOBE (stands for “The Importance Of Being Earnest”) created an index of popularity of various programming languages called TIOBE programming index. Index is calculated based on the number of search requests related to different languages in the six popular search engines – Google, Google Blogs, Yahoo!, MSN, Wikipedia, Baidu and YouTube.
Below are graphs showing the popularity of Swift and Objective-C over time, based on TIOBE data.
This data distinctively proves the growing popularity of Swift, which finds itself increasingly replacing Objective-C in current software projects. However, Objective-C itself also remains very popular and still widely used today.
Which should I as a developer choose?
Objective-C and Swift advantages and disadvantages should all be considered before making a final choice. However, ultimately, both languages are different and shine in different circumstances, therefore, it is important to choose a language on the per-project basis.
Documentation & language support
Since Objective-C existed for many years with current version 2.0 launched in 2006 and available for 10 years as of now, there are a huge number of resources, including books, tutorials and sample code available out there covering the language and implementations of various features.
Thanks to constant support from Apple over the years, Objective-C currently is both very stable and very reliable. When Swift was introduced in 2014, AppStore had more than 1.2 million apps – all of them written in Objective-C.
An incredibly large amount of third-party libraries is written in Objective-C, focusing both on UI and program logic (databases, networking, multithreading, data analysis, etc.).
Swift, on the other hand, doesn’t have as much third party libraries, however, this is only a matter of time, as it is young and evolving language.
Apple themselves strongly promote Swift. Example of applications, entirely written in Swift includes Slideshare by LinkedIn and WWDC by Apple.
At the moment, Swift 3 comes along with Xcode 8, fixing many of the defects and problems of the language and including plenty of improvements.
Runtime in Swift is severely behind when compared to Objective-C, and there is a good chance that it won’t be able to catch up in this regard for years to come. As it stands now, Objective-C is the choice when it comes to creating code that benefits from reflection and deep introspection of types and objects. Such a code is most often used for complex SDK, but can also be occasionally found in apps. However, if robustness of runtime is not your main concern, you will be able to easily achieve your goals with Swift.
Swift is way ahead of the Objective-C when it comes to stability of the code itself. This is thanks to its strong typing system and the way error handling works. By simply avoiding ! operators and following idiomatic Swift, you can easily write a code that will account for all potential errors. There are, of course, some exceptions. One great example of an error that can plague both Swift and Objective-C code is a memory leak from a retain cycle. Main cause of this error is the fact that the way Swift automatically references counting system is left unchanged from Objective-C.
Working with Foundation APIs
Objective-C is a best choice of language is you need to create application that utilizes some foundation APIs. Examples of such APIs include CoreFoundation, CoreAnimation and AVFoundation. While Swift do provide some ways to work with them, such as wrappers that help to smooth out memory management, the fact that the APIs themselves are based on C makes them much better fit for use with Objective-C.
Using C++ Code
Objective-C also much better at working with C++ libraries or cross-patform C++ SDKs, as each time you want to employ another part of C++ library with Swift, you need to bring in additional overhead. C++ simply cannot be imported into Swift files as is, thus requiring you to commit to creating Objective-C++ and Objective-C wrappers. A process that is both tedious and can result in a number of hard-to-fix bugs.
Supported operating system
Swift supports all new versions of MacOS, and iOS, as well as any iteration of tvOS and watchOS. However, if you need to create a project for older systems, (below 10.9 for MacOS and below 7 for iOS), then you are forced to use Objective-C, as Swift doesn’t support them.
Swift projects are arguably much more future-proof than the ones written in Objective-C. Swift usage has greatly expanded since its introduction, and it currently used in a third of all Cocoa open source projects. If the pace stays the same, the number of such projects will equal the number of Objective-C ones. Moreover, many new publications about developing for Apple products, such as blogs and online tutorials are already extensively covering Swift. And in the coming years the number of available information will only continue to grow, to the point where there will be no need to understand Objective-C at all. However, it worth noting that despite the rise of Swift Objective-C is still popular and widely used, thus there is no need to convert your code to Swift right away.
Which projects should be written on Objective-C or Swift ?
Next, we will cover the main points to consider when choosing between Objective-C and Swift. While each of the individual points on this list is very important, in is also worth noting that you should make a choice only by carefully considering a situation as a whole, and not based on any single criteria. Choosing the best language for a project depends on both the specifics of the project and qualification of the team, as well as subjective preferences for a particular language or a set of features.
Knowledge and experience
Knowledge of a particular language and experience of using it are probably the first thing many people considering when choosing a language for a project. If your experience in both Objective-C and Swift is equal, then your choice will depend on other factors mentioned below. However, if you have more experience in one particular language, than it is paramount for you to write production applications in this language. If there are no project requirements that dictate otherwise and no objections from your team, you should always create production apps using tools that you familiar with. This prevents accumulation of technical debt, i.e. unfixed bugs or unimplemented features that will otherwise plague the project as you learn as you go.
However, if you need to create a prototype, using a language that you are least familiar with can be a great choice, as it will allow you to learn new thing while simultaneously keeping all the troubles away from production. Using unfamiliar language is also worth it because it gives you a new perspective on things that you have already done, and it may give you a few good ideas on how to improve things when returning to your core production code.
As mentioned above, using an unfamiliar language will often lead to technical debt that can easily put you out of schedule. If you absolutely need to deliver on time, it is paramount for you to use a language where you have a lot of experience and can precisely estimate how much a particular task will take. If your don’t have much experience working with either Swift or Objective-C, then it is better for you to choose Swift, as it allows for faster development.
Scale of the project
Scale of the project is a very important factor in this case due to immaturity of the Swift language. As language updates with each new version, code needs to be converted, and major changes can introduce a rather large overhead into development process. Thus, it is better to restrict the usage of Swift to smaller projects until it matures a bit more, and use much more stable Objective-C for larger ones.
Supported operating systems
As already mentioned above, if your app need to support older versions of iOS and Mac OS, then you have no choice but to use Objective-C.
Objective-C is best used in:
- applications that require low-level programming
- applications that support OS older that OS X 10.9 / iOS 7
- applications that require or can potentially require a use of C++ libraries or code embedding
- libraries that can be used in cross-platform applications
- large project. Objective-C is old and stable, no major changes in newer versions.
- easy to learn after C / C++
- much more 3rd-party libs and frameworks are available
- lots of documentation and question answers on the net, gathered over 10+ years
- stability and time-proven reliability
- easy integration with C++
- can be using in Qt
- strange syntax
- language with dynamic runtime will always be slower than language with static runtime
Swift is best used in:
- applications targeting OS X 10.9 / iOS 7 and newer
- if don’t have experience in both Objective-C and Swift
- modern code syntax (like C#, Java, Go, Rust)
- Active support and promotion by Apple
- Swift is open source
- Static runtime: faster performance
- Swift is going to see bigger use in the future on Windows and Linux
- Thanks to modern syntax, learning Swift is easier than Objective-C for C# / Java / Go developers
- support only iOS 7+ / OS X 10.9+ apps
- young language: bugs and lack of IDE