As mentioned in an older blog post, Apple surprised many with their announcement of Swift, a new programming language for iOS and OS X, at WWDC 2014. According to Apple, developers will find it easier than before to develop powerful apps as Swift will help them code faster. Swift will remain in the beta version until Apple releases iOS 8 and will replace Objective C for iOS and OS X development.
Also, in the last 10 years, companies such as Microsoft, Facebook and Google have launched new languages for programming. So, Apple’s introduction of Swift has been long overdue.
Let us try to understand what it means for iOS app developers :
Similarities between Swift and Objective C
- There is interoperability between Swift and Objective C and both can be mixed and matched in the same app. Developers don’t have to stick to one language or the other for entire projects; they can add new components written in Swift to existing projects. Also, they can leverage existing libraries in Objective C in any new apps written in Swift.
- You can have typed collections on account of the introduction of Generics. Create code that can deal with multiple types without any duplication of code. That is because generics allow you to create more flexible code. Also, there are fewer code defects as there is run-time safety because the kinds of objects being handled is known.
- Dynamic evaluation in the development environment, in real time. While you are making modifications to the code, you can see how these will affect your code. Xcode Playground lets you replay, fast-forward, rewind and pause while checking the execution of your app, so as to let you have complete control.
- Advanced Operators:
- Detection of underflow or overflow. Use the ‘&’ operator modifier to handle underflow or overflow of your operations.
- Operator overloading. You can implement custom operators in Swift, just like in C++. For string concatenation and for collections, this is built-in already.
- Protection through the ‘&’ symbol against errors. You can prevent errors by using the ‘&’ character as a prefix for operators. For instance, use the ‘&’ operator in place of the ‘/’ operator to avoid errors.
- Range evaluation: Use list evaluation and range operators to make loop constructs simpler.
- Default Parameter Values: Swift reduces the need for code repetition and overloading as it lets functions provide default parameter values.
- Difference between struct and class: Swift structs don’t participate in Automatic Reference Counting and are copied when passed around code, which means that Swift structs are value-types. On account of that, old C-style structures can be encapsulated into Swift and these structures can be passed back to legacy C functions easily.
- Type Inference: Type inference takes up a lot of the burden of declaring type, even as Swift is a typed language. So, you have less boilerplate code, although you still have a type safe environment.
Major differences between Swift and Objective C
Apart from the fact that you can use emoji in your code in Swift, as against Objective C where you can’t, there are several differences between Objective C and Swift.
Naming Variables and Constants
In Swift, you can use Unicode characters, among nearly all other types of characters, for variable and constant names.
However, variable and constant names can’t contain line- and box-drawing characters, mathematical symbols, arrows or invalid or private-use Unicode code points. Also, numbers may be included anywhere in the name, except for the beginning.
Swift is somewhat less verbose than Objective C but that is compensated by nesting of multiline comments in the code to help those who read the code to know what each section is all about.
In Swift, you can nest multiline comments in other multiline comments, which is something that can’t be done in C. You start a multiline comment block and follow that up with beginning another multiline comment in the first block. Then you close the second block before your close the first block.
So, even if the code has multiline comments, you can comment on large code blocks easily and quickly in Swift.
Although you can do that if you want to, you don’t have to follow each code statement with a semicolon in Swift, unlike several other languages. However, semicolons are required if you want to write more than one separate statement on one line.
Floating point remainder calculations
Besides advanced floating-point calculations, Swift operators make arithmetic simpler. These also help avoid errors while writing the operations.
The remainder operator in Swift can operate on floating-point numbers, which is not the case with the remainder operator in C and Objective C.
Swift also provides a shortcut for expressing value ranges through two range operators that are not found in C.
The assignment operator in Swift doesn’t return a value by itself. On the other hand, the assignment operator in Objective C and C returns such a value. This feature keeps the assignment operator from being accidentally used in place of the ‘equal to’ operator and, therefore, prevents errors in the code.
A smaller number of overall package files are required while writing in Swift, as against Objective C. That is because in Swift such files can generate their own interfaces to interact with other parts of the apps of which they form parts of.
Apple has decided to opt for more web-native vocabulary for Swift.
An object is what an instance of a class is traditionally known as. However, structures and classes are closer in functionality in Swift than other languages in programming. Because of this, the term ‘instance’, which is more general, is used.
You don’t have to create separate implementation and interface files for custom structures and classes while programming in Swift, unlike other languages for programming. The external interface to a structure or class is made available for use by other code, when you define a structure or class (in a single file).
In Swift, it is easier to manage the sub-properties of an instance. That is because you can directly set a structure’s sub-properties in Swift, unlike Objective C.
Web developers face fewer problems during debugging in Swift as object inheritance is straightforward in the language. In other languages, they find it difficult to ascertain where their instances/objects inherit properties from.
Subclasses in Swift don’t inherit superclass initializers by default, which is not the case in Objective C. The approach that Swift takes prevents the creation of new instances of subclasses that are not correctly or fully initialized. That is because it does not let simple initializers from superclasses to be inherited by more specialized subclasses.
What do developers think about Swift?
As mentioned above, Swift offers several benefits and is likely to bring about major changes in how developers build apps for the iPad and iPhone.
For both developers and consumers, Swift should be good news. Swift has a syntax that feels familiar and yet is simpler and easier to pick up. It has none of the more complicated trappings of Objective C.
We have spoken to the developers at Net Solutions about this.
Many developers are of the opinion that they can get more work done in lesser time in Swift. They think that they have to do less of ‘housekeeping’ work in Swift than in other programming languages.
They feel that the language has several powerful features that Objective C didn’t have. For instance, Swift lets them see what effect the changes they make has on the end product. On the other hand, in Objective C, they had to wait till the project could be compiled and run before they knew if any changes in code were required.
Also, Objective C and Swift have the same compiler, so an app can have code written in Swift, Objective C and C. This is beneficial for developers who are familiar with the older languages.
The developers say that this can prove very useful because if they want to use features that are available in Objective C but not Swift, they can do so without any problems.
However, they also think that there is room for improvement. For instance, Swift lets users decide if they want to end each of their coding statements with or without a semicolon. This, according to them, could lead to some confusion. New development teams or developers could be hindered by the lack of continuity.
Overall, however, they seem quite impressed with the technology roadmap and features offered by Swift and think that Swift is efficient and quick.
All said and done, it is going to take some time before people get to see apps written in Swift. Apple is not going to accept apps developed in Swift in its app stores before the launch of OS X and iOS 8.
Swift is likely to benefit both developers and consumers. Developers are likely to find it easier to develop apps in the language (especially on account of its interoperability with Objective C) and consumers are likely to benefit from the much quicker functioning of the apps. The beneficial features of Swift are expected to far outweigh a few drawbacks such as the slight confusion that might be caused by the use of semicolons being non-mandatory, while coding in the programming language.
Have you had the opportunity to use Swift as yet? If so, please share your opinion and experience with us in the comments section below.