This is our first episode learning the new language from Apple, Swift.

This week we take a quick look at the new playground and some language basics like constants, variables, variable types, condition statements, loops, while, switch/case, functions, nested functions, functions as arguments and more.

Downloads

Apple HD Apple SD Audio MP3
var str = "Hello, playground"

let goodbye = "Goodbye everyone"

str = "later"

var number: Double = 5

let label = "Today is the "
let dayNumber = 3
let dayDescription = label + String(dayNumber)

let cars = 35
let trucks = 7
let carSummary = "I Have \(cars) Cars On My Lot"
let otSummary = "I Have \(cars + trucks) total vehicles on my Lot"

var colorArray = ["Red", "Green", "Blue", "Yellow", "Pink"]
colorArray[4]
colorArray[4] = "Cyan"
colorArray[4]

var friendList = [
    "Michelle": "DOL-BLS",
    "Mike": "Mechanic"
]

friendList["Jackie"] = "Enterprise Security"

for (friendName, FriendOccupation) in friendList {
    if friendName == "Jackie" {
        friendName
    } else {
        FriendOccupation
    }
}

for var l=0; l<10; ++l {
    l
}

for l in 0..20 {
    l
}

var n: Int = 2

while n < 100 {
    n = n * 2
}

n = 2
do {
    n = n * 2
} while n < 100


let vegetable = "red pepper"
switch vegetable {
    case "celery":
        let veggie = "You are eating crunchy celery"
    
    case "cucumber", "Watermellon":
        let veggie = "You are eating juicy"
    
    case let x where x.hasSuffix("pepper"):
        let veggie = "Is is a spicy \(x)?"
    
    default:
        let veggie = "Everything is good in soup"
}

func theHello(name: String, dayToday: String) ->String {
    return "Hello \(name), how is yout \(dayToday) going?"
}

theHello("Brian", "Wednesday")

func getTemperatures() -> (Double, Double, Double) {
    return (75.5, 81, 53)
}

getTemperatures()

func addTogether(numbers: Int...) -> Int {
    var sum: Int = 0
    for number in numbers {
        sum += number
    }
    
    return sum
}

addTogether(25,11,28)

addTogether(25,11,28,75,92,36)


func addNested(addValue: Int) -> Int {
    var y = 10
    func add() {
        y += addValue
    }
    add()
    return y
}

addNested(50)

func grade(score: Int) -> String {
    switch score {
        case 93..100:
            return "A"
        case 86..92:
            return "B"
        case 79..86:
            return "C"
        default:
            return "F"
    }
}

func swingGrade(score: Int) -> String {
    switch score {
    case 90..100:
        return "A"
    case 80..89:
        return "B"
    case 70..79:
        return "C"
    default:
        return "F"
    }
}

func setGrade(score: Int, grade: Int -> String) -> String {
    return grade(score)
}

setGrade(90, grade)




var numbers = [12, 7, 10, 25, 35]

numbers

numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    if result % 2 == 0 {
        return 0
    } else {
        return result
    }
})


numbers