Jaebi의 Binary는 호남선

Swift - 문법 (Values, Control Flow, Functions and Closures) 본문

Swift

Swift - 문법 (Values, Control Flow, Functions and Closures)

jaebijae 2024. 6. 11. 04:43

목차

    Values

    • var → variable
    • let → constant
    • type 명시 → `let explicitDouble: Double = 70`
    • type conversion → wrap with `Int()`, `Double()`, `String()`
    • String 표기 (string interpolation)
      • 기존 Flutter의 `'${value}'` -> Swift에서는 `"\(value)"`
    • three double quotation marks(`"""`) → multiple line strings
    • arrays and dictionaries → use `[]`
    • array → var fruits = `["limes", "pears", "apples"]`
    • dictionary → var jobs = `["Jae": "Student", "Kai": "Actor"]`

    Control Flow

    • conditionals → `if` and `switch`
    • loops → `for-in`, `while`, `repeat-while`
    for score in individualScores {
    	if score > 50 {
        	teamScore += 3
        } else {
        	teamScore += 1
        }
    }

    Functions and Closures

    • function → `func`
    • use `->` to separate the parameter names and types from the function's return type
    func greet(person: String, day: String) -> String {
        return "Hello \(person), today is \(day)."
    }
    • use underscore instead of an explicit argument label to omit the parameter
    func someFunction(_ firstParameterName: Int, secondParameterName: Int) {}
    someFunction(1, secondParameterName: 2)
    • default parameter values
    func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {}
    someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
    someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12
    • function types
    var mathFunction: (Int, Int) -> Int = addTwoInts
    Define a variable called mathFunction, which has a type of ‘a function that takes two Int values, and returns an Int value.’ Set this new variable to refer to the function called addTwoInts.
    • nested functions
    func returnFifteen() -> Int {
        var y = 10
        func add() {
            y += 5
        }
        add()
        return y
    }
    • function can return another function as its value
    func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
        for item in list {
            if condition(item) {
                return true
            }
        }
        return false
    }
    func lessThanTen(number: Int) -> Bool {
        return number < 10
    }
    var numbers = [20, 19, 7, 12]
    hasAnyMatches(list: numbers, condition: lessThanTen)
    • closure → `({})`
    • use in to separate the arguments and return type from the body in closure
    numbers.map({ (number: Int) -> Int in
        let result = 3 * number
        return result
    })
    • if closure’s type is already known, can omit the type of its parameters, its return type, or both
    let mappedNumbers = numbers.map({ number in 3 * number })
    print(mappedNumbers)
    // Prints "[60, 57, 21, 36]"
    • when a closure is the only argument to a function, can omit the parentheses
    let sortedNumbers = numbers.sorted { $0 > $1 }
    print(sortedNumbers)
    // Prints "[20, 19, 12, 7]"

     

    Reference

     

    Documentation

     

    docs.swift.org