Swift 2.0 for programmers Part#4


Points to be noted

  • Int, Bool, Double, String, Array, Dictionary are all structures, not class
  • struct is identical to class. Change a class keyword to struct keyword
    and everything should work fine

Differnece between struct and class

  • Structures are value types (pass by value) unlike class
  • Swift generates member wise intializer for struct. Member wise initializer is a default init method, that contains all the member variable as parameters
  • struct do not take part in Inheritance
  • No deinit in struct


var a = 1   // Good
var b=1     // Also good
var c= 1    // Not good, compile error
var d =1    // Not good, compile error
a++         // Good
a--         // Good
++a         // Good 
--a         // Good

Points to be noted

  • Space on either side of the operators must be balanced.

Nil coalescing operator

var name : String?
var defaultName = "John Doe"

var personName = name ?? defaultName

Points to be noted

name is a optional variable, it may or may not have values. So when we are using name to initialize personName then we are running into a trouble. Nil coalescing operator in on rescue. If name has a value then personName will be initialized with it, otherwise defaultName will be used.

Type check

let button = UIButton()
let label = UILabel()
let view = UIView()

let collections = [button, label, view]

for item in collections {
    // we are checking if the "item" is UIButton type
    if item is UIButton {
        // downcasting the item to UIButton
        let aButton = item as! UIButton
        // now we can access UIButton specific behavior
        aButton.setTitle("Hello", forState: UIControlState.Highlighted)
    // Compact way to achieve the same objective as presented before
    if let bButton = item as? UIButton {
        bButton.setTitle("Hello", forState: UIControlState.Highlighted)


protocol OnClickListener {
    func click()
    func longClick()
    func getColor() -> String
    // real only property
    var clickCount : Int {get}
    // read-write property
    var text : String {get set}

class ImplementationClass: OnClickListener {
    init () {
    func click() {
    func longClick() {
    func getColor() -> String {
        return "RED"
    var clickCount : Int {
        return self.clickCount
    var text : String {
        get {
            return self.text
        } set {
            self.text = newValue

var mImpl = ImplementationClass()
mImpl.text = "Button Name"


extension Int {
    func add(a : Int) -> Int {
        return self + a

var a = 5
let res = a.add(3)


func printArray<T>(array: [T]) -> T {
    for item in array {
        print(" ")
    return array [array.count - 1]

let lastInt = printArray([1, 2, 3])
let lastString = printArray(["Hello", "World", "Swift"])

=================== The End ===================

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.