What’s interesting is that Swift allows you to use nearly any character for both variable and constant names. You can even use emoji character for the naming:
Tip: You may wonder how you can type emoji character in Mac OS. It’s easy. Just press Control-Command-spacebar and an emoji picker will be displayed.
You may notice a huge difference in variable declaration between Objective C and Swift. In Objective-C, developers have to specify explicitly the type information when declaring a variable. Be it an int or double or NSString, etc.
It’s your responsibility to specify the type. For Swift, you no longer needs to annotate variables with type information. It provides a huge feature known as Type inference. The feature enables the compiler to deduce the type automatically by examining the values you provide in the variable.
It makes variable and constant declaration much simpler, as compared to Objective C. Swift provides an option for you to explicitly specify the type information if you wish. The below example shows how to specify type information when declaring a variable in Swift:
No Semicolons:
In Objective C, you need to end each statement in your code with a semicolon. If you forget to do so, you’ll end up with a compilation error.
As you can see from the above examples, Swift doesn’t require you to write a semicolon (;) after each statement, though you can still do so if you like.
Basic String Manipulation:
In Swift, strings are represented by the String type, which is fully Unicode-compliant. You can declare strings as variables or constants:
In Objective C, you have to choose between NSString and NSMutableString classes to indicate whether the string can be modified. You do not need to make such choice in Swift. Whenever you assign a string as variable (i.e. var), the string can be modified in your code.
Swift simplifies string manipulating and allows you to create a new string from a mix of constants, variables, literals, as well as, expressions. Concatenating strings is super easy. Simply add two strings together using the “+” operator:
Swift automatically combines both messages and you should the following message in console. Note that println is a global function in Swift to print the message in console.
You can do that in Objective C by using stringWithFormat: method. But isn’t the Swift version more readable?
String comparison is more straightforward. In Objective C, you can’t use the == operator to compare two strings. Instead you use the isEqualToString: method of NSString to check if both strings are equal. Finally, Swift allows you to compare strings directly by using the == operator.
Arrays:
The syntax of declaring an array in Swift is similar to that in Objective C. Here is an example:
Objective C:
Swift:
While you can put any objects in NSArray or NSMutableArray in Objective C, arrays in Swift can only store items of the same type. Say, you can only store strings in the above string array. With type inference, Swift automatically detects the array type. But if you like, you can also specify the type in the following form:
Like NSArray, the Swift provides various methods for you to query and manipulate an array.
Simply use the count method to find out the number of items in the array:
In Objective C you use the addObject: method of NSMutableArray to add a new item to an array. Swift makes the operation even simpler. You can add an item by using the “+=” operator:
This applies when you need to add multiple items:
To access or change a particular item in an array, pass the index of the item by using subscript syntax just like that in Objective C.
One interesting feature of Swift is that you can use “…” to change a range of values. Here is an example:
This changes the item 2 to 4 of the recipes array to “Cheese Cake”, “Greek Salad” and “Braised Beef Cheeks”.
Dictionaries:
Swift only provides two collection types. One is arrays and the other is dictionaries. Each value in a dictionary is associated with a unique key. If you’re familiar with
NSDictionary in Objective C, the syntax of initializing a dictionary in Swift is quite similar. Here is an example:
Objective C:
Swift:
The key and value in the key-value pairs are separated by a colon. Like array and other variables, Swift automatically detects the type of the key and value. However, if you like, you can specify the type information by using the following syntax:
To iterate through a dictionary, use the for-in loop.
You can also use the keys and values properties to retrieve the keys and values of the dictionary.
To access the value of a particular key, specify the key using the subscript syntax. If you want to add a new key-value pair to the dictionary, simply use the key as the subscript and assign it with a value like below:
Now the companies dictionary contains a total of 5 items. The “TWTR”:”Twitter Inc” pair is automatically added to the companies dictionary.
Classes:
In Objective C, you create separate interface (.h) and implementation (.m) files for classes. Swift no longer requires developers to do that. You can define classes in a single file (.swift) without separating the external interface and implementation.
To define a class, you use the class keyword. Here is a sample class in Swift:
Similar to Objective C, right? In the above example, we define a Recipe class with three properties including name duration and ingredients. Swift requires you to provide the default values of the properties. You’ll end up with compilation error if the initial values are missing.
What if you don’t want to assign a default value? Swift allows you to write a question mark (?) after the type of a value to mark the value as optional.
In the above code, the name and ingredients properties are automatically assigned with a default value of nil. To create an instance of a class, just use the below syntax:
You use the dot notation to access or change the property of an instance.
Swift allows you to subclass Objective-C classes and adopt Objective-C protocols. For example, you have a SimpleTableViewController class that extends from UIViewController class and adopts both UITableViewDelegate and UITableViewDataSource protocols. You can still use the Objective C classes and protocols but the syntax is a bit different.
Objective C:
Swift:
Methods:
Swift allows you to define methods in class, structure or enumeration. I’ll focus on instance methods of a class here. You can use the func keyword to declare a method. Here is a sample method without return value and parameters:
In Objective C, you call a method like this:
In Swift, you call a method like this:
If you need to declare a method with parameters and return values, the method will look this:
The syntax looks a bit awkward especially for the -> operator. The above method takes a name parameter in String type as the input. The -> operator is used as an indicator for method with a return value. In the above code, you specify a return type of Int that returns the total number of todo items. Below demonstrates how you call the method:
Control Flow:
Control flow and loops employ a very C-like syntax. As you can see above, Swift provides for-in loop to iterate through arrays and dictionaries. You can use if statement to execute code based on a certain condition. Here I’d just like to highlight the switch statement in Swift which is much powerful than that in Objective C. Take a look at the following sample switch statement. Do you notice anything special?
Yes, switch statement can now handle strings. You can’t do switching on NSString in Objective C. You had to use several if statements to implement the above code. At last Swift brings us this most sought after utilization of switch statement.
Another enhancement of switch statement is the support of range matching. Take a look at the below code:
The switch cases now lets you check values within a certain range by using two new operators: “…” and “..”. Both operators serve as a shortcut for expressing a range of values.
Consider the sample range of “41…70″, the … operator defines a range that runs from 41 to 70, including both 41 and 70. If we use the .. operator instead of … in the example, this defines a range that runs from 41 to 69. In other words, 70 is excluded from the range.
Please do post your comments.
DHARANI KUMAR REDDY A