Swift for iOS app development is being adopted by many top organizations like American Airlines, Getty Images, LinkedIn and Duolingo for obvious technical and business benefits – ranging from enhanced productivity to minimized bugs in their shipping apps with interest in this new programming language slowly rising.
In order to catch up with the trend brewing in the techno-corporate world and to generate more value for its customers,Innovapptive Inc., a SAP mobile application development and services partner has adopted Swift as a programming language for iOS development.
What is so great about Swift that corporates are slowly switching from Objective-C to Swift? This blog explores these aspects from a technical perspective – high level comparison of Swift over Objective-C and finally delves into the core business benefits that Innovapptive had accrued by adopting Swift.
What is Objective-C?
Objective-C is a general purpose, object oriented programming language that incorporates Smalltalk type messaging to the “C” programming language. This is the core programming language that is used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs) such as Cocoa and Coca Touch.
However, there was lot of complexity involved in Objective C pertaining to concepts like declaring constants, arrays and hash tables, apart from non-suitability for high-performance code, prompting developers to revert to C or C++ in most of the cases. This was causing a major hiccup for Apple, particularly at a time, when it is planning a massive proliferation of Apple products in the market that rank high in performance and efficiency.
Emergence of Swift programming language
After a thorough research, Apple introduced Swift, a new programming language at Apple’s 2014 Worldwide Developers Conference (WWDC). This new programming language is slowly revolutionizing the way applications are built on Apple iOS, offering a concise, yet expressive syntax, enabling apps to run at lightning speed. One great thing about Swift code is it works side by side, along with Objective – C.
Flexibility of Swift over Objective C
Elimination of .h and .m files
Header files was a major burden in C. Instead, Swift offers flexibility to keep all code contained in .swift files. This implies a 50% reduction in file count for your projects!
Elimination of brackets
As with any high level programming language, Objective-C has a tendency to make programming complex due to its unique bracket-based syntax. Instead, Swift uses parenthesis and named parameters, similar to that in C#.
No more pluses and minuses
Instance methods are prefixed with a minus and class methods are prefixed with a plus in Objective-C, whereas in Swift, methods are prefixed with a func keyword and class methods (“type methods”) are additionally prefixed with the classkeyword.
No need to use semicolons
Unlike in Objective “C”, you can still use semicolons to terminate sentences, but they are not required. The only instance where you need to use semicolons is when you have multiple statements on a single line.
No need to use more asterisks
C pointer syntax is a domain that is really daunting for new developers. Swift eliminates this usage; it doesn’t provide direct access to pointers. However, there are still various pointer types available under its belt, when you need direct access to memory, for instance, CMutablePointer.
Type inference is a simple method to specify a variable’s type at compile-time without being explicit. This is executed using the var keyword, just like in C#. However, Swift offers “catch-all” data types: AnyObject can represent an instance of any class type and Any can represent an instance of any type at all (not just function types).
Objective-C provides quite a flexibility with data types. For instance, an NSArray can contain multiple types of objects at once. This may seem simple and flexible, but it makes the code more error-prone and transfers much type checking to runtime. Swift offers safety of strongly typed variables, which means as many of these errors can be easily caught at compilation time. Generics, for instance, enables collections such as arrays to be strongly typed, enabling them to contain only a particular type of the object. This is enforced at compilation time.
Strings are value types
Strings are value types in Swift and not reference types, unlike in Objective-C. You can easily compare using the == operator or concatenated using the + operator. String values are also copied when passed into a function.
A taste of functional programming
Multiple return types
In Objective-C and other C-based languages, functions can only return one value, whereas in Swift, it permits any number of return types called tuples, opening up new avenues for a cleaner and a leaner code.
A different variant of nil
In Objective-C, nil represents a pointer to nothing and hence restricts usage for value types such as ints, structs orenums. Swift introduces optionals, which are similar to nullable types in C#.
If statements require booleans
The nil or the number 0 evaluates to false and any other value to true in C-based if statements. This lets developers to perform shortcuts like if (array.count), where any value other than zero will return true. In Swift, in an if statement, the conditional must be a bona-fide Boolean expression like if (array. count > 0). This may eliminate certain code shortcuts, but lets the code become more explicit and safer.
Different forms of Switch statements
Switch statements are great branching mechanisms, and much better than if/else if there are more than a few conditions.
Objective-C limits case comparisons to integer comparisons and equality tests. Swift enables string and complex object comparisons. Apart from that, switch statements in Swift always exit as soon as they find a matching case by default; this makes break keyword no longer necessary.
Elimination of boxing and unboxing
Swift Arrays eliminates the usage of boxing and unboxing – a concept widely used in C by natively supporting value types as well as reference types.
Only two types of collections
Unlike Objective-C with many different collection types (NSSet, etc) as well as their mutable variants, Swift has only 2 collection types: arrays and dictionaries and are mutable by default. They can be made immutable by using of the let keyword. You can change the contents of the immutable arrays as long as the array size does not change. You cannot change the value of immutable dictionaries.
Generics are a powerful method to write code that is strongly typed, but flexible. Similar to C#, Swift uses where after the type name to denote a list of requirements like requiring the type to inherit from a particular class or implement a certain protocol.
The worst feature that one might encounter in Objective-C is its lack of support for namespaces. In order to minimize the chance of name collision, class names are made “unique” that is done by prefixing the class name with 2 or 3 letters. In Swift, if multiple imported modules share a member of the same name, it can be made unambiguous by prefixing it with the module name:
Properties and instance variables
Swift integrates properties and instance variables into a single class-level variable declaration, putting an end to a debate among the Objective-C developers. A Swift property does not consist of a corresponding instance variable and the backing store for a property is not accessed directly. You can use explicit getters and setters for compound/derived properties. On the other hand, Swift offers property observers via the new willset and didset hooks that run before and after setting a new value.
No access control mechanisms
One striking drawback in Swift is it lacks access modifiers (public/private/protected), which hinders access to classes’ members. Presently, all code in Swift is public that does not allow proper encapsulation.
Most classes ultimately inherit from NSObject in Objective-C, whereas, in Swift, this is not required. Swift supports single inheritance only, just like in Objective-C.
In contrast to Objective-C initializers, Swift initializers do not return a value. A Swift initializer is similar to an instance method with no parameters, written using the init keyword. Every class must have at least one designated initializer.
Protocols are highly important for low coupling between classes. Swift extends the same feature to value types as well; classes as well as enumerations and structs can all adopt protocols. Protocols are also bona-fide types and you can use them in return types and parameters.
Categories are called extensions
Objective-C categories are called as extension methods in most languages. Swift uses this same nomenclature with theextension keyword. However, the major difference is that swift extensions are not named, just like in Objective-C, making their purpose unclear.
Now that you had got a quick overview of the flexibility aspects of Swift programming language over Objective-C, the last leg of this blog will help you understand the core business benefits that Innovapptive had accrued by adopting Swift.
- Reduction in development efforts by 30% by way of user friendly and simpler coding mechanism.
- Quick roll out of applications enabling innovative iteration, while instantly responding to user requests for new capabilities.
- Higher quality code that’s easier to maintain including related performance enhancements.
- Minimized training and adoption efforts for developers – higher productivity within short span of time.
- 70% reduction in bugs count – Swift won’t let you compile code with common errors.
From the above discussion, it certainly boils down to the fact that Swift scores high over Objective-C and incorporates modern features, inspired by more recent languages. It can be termed as a “safer” language implying that you can easily catch more errors at compile time as compared to the run time. This blog just made an attempt to cover certain basic and core features, though there are some more features that aren’t dealt in this blog. The main focus of this blog is to provide a general awareness and cultivate interest among programmers and corporates to switch over to Swift code from a technical and business perspective – generate more value to your customers.
If you would like a demo of Innovapptive’s portfolio of Native or Web based mobile solutions, please click on the link. Alternatively, if you would like to discuss with an Innovapptive solution expert, you can reach out to us by emailing us at firstname.lastname@example.org or you can reach a sales representative at (713) 275-1804.