Swift is an emerging language with a great combination of cleaner version perhaps a familiar syntax, modern language and superb backward compatibility features, which makes it better while developing applications for iOS and OS X apps. It is built with LLVM framework included in Xcode 6.
The vital ability provided by Apple in Swift is Type inference, as programmer does not need to annotate values with its type whether value is constant or variable. Lets understand this with an example:
We have a value which depicts a “name”.
let name = “John” // automatically inferred
let name : String = “John” //explicit case
In Swift , String is as much powerful as NSString in Objective C whereas “let” defines that you have declared the value as constant value.
var integerValue = 42 //automatically inferred variable integer value
Now, here variable means values can be changed as per the requirement.
Interestingly, Swift gives the utility to use emoticons and no use of semi colons. Programmer can make combinations of string,characters.
To use string interpolation ,simple wrap parentheses around any type you want to include in the strings and prefix it with a backslash.
let friendName = “Daniel”
var commonFriends = 5
let result = “My Friend name is \(friendName) and we have \(commonFriends) friends in common.”
// Output = My Friend name is Daniel and we have 5 friends in common.
Swift Featuring – String Mutability
Swift provides mutability features for String and also for its Collection Types (Dictionary, Array) which make it relatively easy for programmers.
var variableString = “Some string”
variableString += “has literal value”
variableString = variableString + “has literal value”
//result – Some String has literal value
Similarly , it provides mutability of Arrays and Dictionary. Simple declaration of Array and Dictionary is as follows :-
var names = [“Brian”, “John”, “Melan”] //String array
var numberOfBooks = [“Brian” : 5, “John” : 4, “Melan” : 5]
//Dictionary with String type key and correspondent integer values.
Now the question arises, if we need to modify or mutate these collection types – how can we do that? Here is the example which explains the solution –
names += [“Edward”] // Addition of single element in names Array
names [5…7] = [“Celesty”, “Maya”, “Vella”]
//Addition of elements in names Array with particular range
numberOfBooks [“Vella”] = 10
// Addition of key “Vella” with value 10 in nuumberOfBooks Dictionary
numberOfBooks [“Melan”] = 8
// Modifying value for key “Melan” in nuumberOfBooks Dictionary
On Contrary to Objective c , Swift does not create pointers and unsafe accessors. It is designed in a way that it never let programmer face exceptions.
There are number of such utilities provided by swift language for developers.Some of these are :-
Swift provides multiple returning value functions, or it can be explained as one return value containing tuples at calling site.
Swift uses ARC (Automatic Reference Counting) to manage memory. ARC consists of Initialization and deinitialization of objects. It also enhances the tool to protect against mistakes and bugs,providing feature like Playground. Playground helps the programmer to have look on the results immediately while programming with interactive views.
The key element of Swift is the ability of clean debugging and run within developing environment.