By: Irna Fitriani, Mitrais Analyst Programmer
In June 2014 Apple released Swift as another alternative for the software developer to develop applications for native iOS. Since then the Swift language has iterated rapidly. Swift has released several major and minor versions and released Swift 3.0.1 in October 2016.
Critics of Swift claim the product is immature compared to Objective – C, especially for medium to large projects. Before deciding which one we should we learn first, let’s explore the reason that Apple created Swift in the first place.
Why Apple Created Swift
Apple’s company culture and vision is "an obsession with end-to-end design and seamless hardware –software-development integration."
However, the original native iOS language, Objective–C attracted a lot of criticism as it is notoriously hard to learn for a beginner.
Apart from being much easier to learn, Swift is fast.
Swift was developed as a replacement for C-based languages (C, C++, and Objective-C). As such, Swift must be comparable to those languages in performance for most tasks. Performance must also be predictable and consistent, not just fast in short bursts that require clean-up later. There are lots of languages with novel features - being fast is rare.
Swift is easier
Objective-C suffers from all of the warts you’d expect from a language built on C. To differentiate keywords and types from C types, Objective-C introduced new keywords using the @ symbol. Because Swift isn’t built on C, it can unify all the keywords and remove the numerous @ symbols in front of every Objective-C type or object-related keyword.
Swift drops legacy conventions. Thus, you no longer need semicolons to end lines or parenthesis to surround conditional expressions inside if/else statements. Another large change is that method calls do not nest inside each other resulting in brackets. Method and function calls in Swift use the industry-standard comma-separated list of parameters within parentheses. The result is a cleaner, more expressive language with a simplified syntax and grammar.
Swift is safer
In Objective-C, if a value was returned from a method, it was the programmer’s responsibility to document the behavior of the pointer variable returned (using comments and method-naming conventions). In Swift, the optional types and value types make it explicitly clear in the method definition if the value exists or if it has the potential to be optional (that is, the value may exist or it may be null).
To provide predictable behavior Swift triggers a runtime crash if a null optional variable is used. This crash provides consistent behavior, which eases the bug-fixing process because it forces the programmer to fix the issue right away. The Swift runtime crash will stop on the line of code where a null optional variable has been used. This means the bug will be fixed sooner or avoided entirely in Swift code.
Swift requires less code
Swift only needs 1 file which is .swift compared to Objective-C which uses 2 files .h and .m and of course it requires less code than Objective-C. For example, in Objective-C working with text strings is very verbose and requires many steps to combine two pieces of information. Swift adopts modern programming language features like adding two strings together with a “+” operator, which is missing in Objective-C.
Swift: The more approachable, full-featured language
With Swift, programmers have half as many code files to maintain, zero manual code synchronization, and far less punctuation to mis-type - leading to more time spent writing quality lines of code.
Code is now self-documenting in Swift with the addition of optional types: a compile-time safety mechanism for returning a value or no value, which is a common issue with asynchronous operations, network failures, invalid user input, or data validation errors. ARC is unified in Swift between both procedural C-style code, as well as object-oriented code using Apple’s Cocoa framework.
Developers will find that they write less code in Swift, and modern language features support them in keeping lines of code more readable. Swift will keep the entire Apple ecosystem at the forefront of programming as it continues to evolve, thanks to dynamic library support in iOS and Swift. Open source projects, third-party SDKs, and frameworks that integrate with home automation, devices, and social services will be easier to integrate without increasing build times. Swift is almost as fast as C++ in some algorithms.
Add to that the fact that Playgrounds and Swift enable a new way to program with visual feedback that assists the development of algorithms using inline data visualizations. A shorter feedback loop and graphical descriptions make the iterative coding process even easier to start.
Areas where Objective-c still better than swift
- If you need to interact with an old C/C++ library, Objective-C is just way better.
- If you are doing a lot of Video or Audio work. This can be done 100% with Swift, but I just find those iOS Libraries are a bit old and crusty and really lend themselves better to Objective-C.
Ultimately, Swift is a more approachable full-featured programming language that will allow developers to not only build apps but also target embedded systems like the new lower-power Apple Watch for many years to come.
Disclaimer: This article is an independent Mitrais publication and has not been authorized, sponsored, or otherwise approved by Apple Inc