Actually Swift is an alternate language to Objective-C that it has same concepts and syntax, but the difference is that of accessing and calling of method will be replaced with the dot notation instead of namespace. Swift is fast, modern, safe and interactive programming language
The Swift programming language is very easy to those who are familiar with web development technologies and scripting languages like Ruby, Python and Java Script.
In this tutorials you will be knowing about all the basics of swift concepts, such as declaring variables, functions/methods and collections.
So let's go into the basics, in oder to learn basics you no need to create new Xcode project but open Xcode and choose Get started with a playground.
1) What are the let and var in Swift?
let : let is a keyword, is used to declare constant variables or object. When you use let keyword to any variable, you can't change it's value once it's being assigned a value during declaration. More over we can say it as a immutable variable or object in Objective-C
Example:
let price = 12.0
Here in this example price is a variable holding double value, though we didn't defined price as float the compiler will automatically assumes it as double.
If you forcefully tries to change the value of price, it will leads to the following error along with saying suggestion at the console log.
So the conclusion of let keyword is, use the let when ever you want to declare constant variables.
So the conclusion of let keyword is, use the let when ever you want to declare constant variables.
var : let is a keyword, which functions exactly opposite to that of let. When you use var keyword to any variable or object, you can change it's value once it's being assigned a value during declaration. More over we can say it as a mutable variable or object in Objective-C
Example:
var age = 25
age = age + 1 //will be 26 after one year
So the conclusion of var keyword is, use the var when ever you want to declare mutable objects or variables.
2) Collections
Collections are well known for saving multiple values in the same object, for example arrays, dictionary and sets.Let's see how to declare and accessing collections in swift.
Arrays: The array is an object which is used to save collection of data inside it and data can be retrieved based on index, the index is represented by numeric value. The array can be declared in two ways
Case1
let names = ["Peter","John","Smith"]
Case2
let countries:NSArray = NSArray(objects: "US","UK","RSA")
Dictionary: The dictionary is an object which is used to save collection of data inside it and data can be retrieved based on key, the index is represented by numeric value. The dictionary can be declared in two ways.
Case1)
let userInfo = ["name":"Elina","age":"25"]
print("user name is \(userInfo["name"]!)")
Case2)
let person = NSDictionary(objects: ["Peter","40"], forKeys: ["name","age"])
print("the age of \(person.objectForKey("name")!) is \(person.objectForKey("age")!)Yrs")
Click more about collection
3) Special symbols as variables?
Yes, Swift has the feature of supporting special symbols as variable.Ex:
//variables with special symbols
let 🐎 = "i'm horse"
print(🐎)
let 💤 = "sleeping"
print(💤)
To launch the emoji window you can press ctrl+cmd+space
4) How to declaring class in swift?
Before going to the details let's know about what is the class in Swift, a class is building block of our code where we can declare variables and methods to achieve certain functionality or task.
In Swift programming language when you create a new class it would generates only one file with file extension as .swift. You can see the class declaration syntax below.

class X : Y {
var title: String?
var imageName: String?
override init() {
super.init()
}
init(aTitle:String?) {
super.init()
self.title = aTitle
}
convenience init(aTitle:String?, aImageName:String?) {
self.init(aTitle: aTitle)
self.imageName = aImageName
}
}
- init(aTitle:String?, aImageName:String?) cannot call init(aTitle:) and still be a designated initializer, it must be a convenience initializer.
- self.init must be before anything else in init(aTitle:String?, aImageName:String?).
- initializer must be passed the parameter name self.init(aTitle) must be self.init(aTitle: aTitle).
0 Comments