Udemy iOS by Angela Yu / Section 13 / API, Closures

Garam·2023년 7월 8일
0

Udemy iOS by Angela Yu

목록 보기
9/9
post-thumbnail

API (Application Programming Interface)


  • A set of commands, functions, protocols, objects that programmers can use to create software or interact with an external system.
  • Provides standard commands for performing common operation
    (따라서 처음부터 코드를 쓰지 않아도 된다.)
  • 외부 앱으로부터 정보를 끌어올 수 있음
    (계약과 같이, API provider가 제공하는 규칙을 따르면서)



Networking


  1. Create a URL
  2. Create a URLSession
  3. Give URLSession a task
  4. Start the task



Closures


Anonymous functions or functions without a name.


func calculator (n1: Int, n2: Int, operation: (Int, Int) -> Int) -> Int {
    return operation(n1, n2)
}


func add (no1: Int, no2: Int) -> Int {
    return no1 + no2
}


calculator(n1: 2, n2: 3, operation: {(no1, no2) in no1 * no2})
// ==
calculator(n1: 2, n2: 3, operation: {$0 * $1})
// == (if the last parameter in your function is a closure({}로 묶여있는), we can omit the parameter number.
// == Trailing closure
let result = calculator(n1: 2, n2: 3) {$0 * $1}

print(result)

>>> 실행결과
6
  • Type inference works for no1, no2 (according to the inputs)
  • Anonymous parameter: $0 (the first parameter) $1 (the second parameter) ...




let array = [6, 2, 3, 9, 4, 1]

func add0ne (n1: Int) -> Int {
    return n1 + 1
}

array.map(add0ne)
// ==
array.map({$0 + 1})
// ==
array.map{$0 + 1}

>>> 
[7, 3, 4, 10, 5, 2]



array.map{"\($0)"}
>>>
["6", "2", "3", "9", "4", "1"]


  • map: Returns an array containing the results of mapping the given closure over the sequence’s elements.



0개의 댓글