Thursday, November 17, 2016

Swift 3 by example

//: Playground - noun: a place where people can play

import UIKit

// Mutable variable
var str = "Hello, playground"

// Array
var myarr : [String] = ["Watsh", "Rajneesh"]


// Dictionary
var mydict : [String:String] = ["tic": "tac", "ping": "pong"]

// constants
let conststr = "My Const String"
let countup = ["one", "two"]
let mapNameToParkingSpace = ["Alice": 10, "Rajneesh": 12]

// subscript of array
let elem = countup[1]


// Initializers
let emptyString = String()
let emptyArr = [Int]()

let emptySet = Set<Float>()

let defaultNum = Int()

let defaultBool = Bool()

let defaultFloat = Float()


// initializing a set
let availableRoomsSet = Set([1,2,3, 4])


// Properties
emptyArr.count
availableRoomsSet.count
conststr.isEmpty


// Optionals - can be value of the said datatype or nil
var anOptionalFloat: Float?
var anOptionalArrayOfStrings: [String]?
var anOptionalArrayOfOptionalStrings: [String?]?

var reading1: Float?
var reading2: Float?

reading1 = 1.0
reading2 = 2.0

// to use the value we need to unwrap the optional

// 1. forced unwrapping - used when sure that value will not be nil
// if reading2 assignment commented - this will result in error

let avgReading = (reading1! + reading2!)/2

// 2. Optional binding - safe to use always
//  if reading2 assignment is commented out, will go to else block
if let r1 = reading1,
    let r2 = reading2 {
    let avg = (r1 + r2)/2
} else {
    print("reading nil")
}

// Subscripting dictionary
let nameByParkingSpace = [13: "Alice", 27: "Bob"]
let space13Assignee: String? = nameByParkingSpace[13]
let space42Assignee: String? = nameByParkingSpace[42]

if let space13Assignee = nameByParkingSpace[13] {
    print("Key 13 is assigned in the dictionary!")
}


// looping
let range = 0 ..< countup.count

for i in range {
    let string = countup[i]
    // Use 'string'
}


for string in countup {
    // Use 'string'
}

// return a tuple of index and value in array
for (i, string) in countup.enumerated() {
    // (0, "one"), (1, "two")
}


// iterating dictionary
for (space, name) in nameByParkingSpace {
    let permit = "Space \(space): \(name)"
    print(permit)
}

// enum
enum PieType {
    case Apple
    case Cherry
    case Pecan
}

let favoritePie = PieType.Apple

let name: String

// switch-case with enum
// no break required as only the code within the matching case is executed so break is implicit
// fallthrough behavior can be set
switch favoritePie {
case .Apple:
    name = "Apple"
case .Cherry:
    name = "Cherry"
case .Pecan:
    name = "Pecan"
}

let osxVersion: Int = 7
switch osxVersion {
case 0...8:
    print("A big cat")
case 9:
    print("Mavericks")
case 10:
    print("Yosemite")
default:
    print("Greetings, people of the future! What's new in 10.\(osxVersion)?")
}


// Value associated with enum
enum PieTypeInt: Int {
    case Apple = 0
    case Cherry
    case Pecan
}


No comments:

Popular micro services patterns

Here are some popular Microservice design patterns that a programmer should know: Service Registry  pattern provides a  central location  fo...