Sunday, February 7, 2016

Swift Tutorial - Part 2 - Swift Foundation Frameworks

Swift Foundation Frameworks


Video Description

var

In order to define a variable in swift we use var at the beginning of the variable then the name of the variable and at the end we define the type of the variable like bellow:
var myVariable:String = "Some String Value"

let

If the value of a variable doesn't change during it's life cycle, we should use let instead of var, otherwise compiler would show warning. We define a variable with let like bellow: 
var myVariable:String = "Some String Value"

print

In order to print on console we can use print function in swift, here is the example:
print("This text will be printed in console after running the application")

String

From the name of this class it's obvious that it's for defining strings. Here is an example:
var myVariable:String = "Some String Value"

Double

let version: Double = 1.0
print(version)

Int

let myInteger: Int = 2
print(myInteger)

Bool

let isAwesome: Bool = true
print(isAwesome)

Array

We have 2 ways to define an array, here is the first one:


var letters = Array<String>()
letters = ["a", "b", "c", "d"]
print(letters[2])

The second way is to just define it like bellow:



var letters = ["a", "b", "c", "d"]
print(letters[2])

We can iterate the array objects like bellow:


var numbers = [Int]()
numbers = [1, 2, 3, 4]
        
for number in numbers {
   print(number)
}

Dictionary

We have 2 ways to define a dictionary, here is the first one:
var dict = Dictionary<String, Int>()
dict = ["One": 1, "Two":2]

The second way is to just define it like bellow:
var dict = [String:Int]()

We can iterate through keys and values of a dictionary like bellow:
for (key, value) in dict {
   print("Value for \(key) is \(value)")
}

NSObject, NSNumber, NSData, NSDate

These classes are inherited from Objective-c language and we will talk about them later

No comments:

Post a Comment