Skip to content

Instantly share code, notes, and snippets.

// AppDelegate.swift
import UIKit
@UIApplicationMain // UIApplicationMain annotation이 있기 때문에 앱에서 AppDelegate.swift를 앱과 시스템을 연결하기 위한 파일로 인식한다.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
var customersInLine = ["Barry", "Daniella"]
var customerProviders: [() -> String] = [] // 클로저를 저장하는 배열을 선언
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) {
customerProviders.append(customerProvider)
} // 클로저를 인자로 받아 그 클로저를 customerProviders 배열에 추가하는 함수를 선언
collectCustomerProviders(customersInLine.remove(at: 0)) // 클로저를 customerProviders 배열에 추가
collectCustomerProviders(customersInLine.remove(at: 0))
print("Collected \(customerProviders.count) closures.") // 2개의 클로저가 추가 됨
// customersInLine is ["Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0)) // "Now serving Ewa!"
// customersInLine is ["Alex", "Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: { customersInLine.remove(at: 0) } ) // "Now serving Alex!"
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count) // 5
let customerProvider = { customersInLine.remove(at: 0) } // 해당 코드가 지나도 count가 줄지 않는다.
print(customersInLine.count) // 5
// customerProvider가 실행되었을때만 동작
print("Now serving \(customerProvider())!") // "Now serving Chris!"
print(customersInLine.count) // 4
var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure() // 함수 안에서 끝나는 클로저
}
class SomeClass {
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
return incrementer
}
let plusTen = makeIncrementer(forIncrement: 10)
protocol Multipliable {
static func * (lhs: Self, rhs: Self) -> Self
}// 따로 정의해야할 기능이 없다면 빈 프로토콜로 만들어도 된다. (타입을 묶기 위한 용도)
extension Int: Multipliable{}
extension Double: Multipliable{}
infix operator ** : MultiplicationPrecedence
func **<T>(lhs: T, rhs: T) -> T where T:Multipliable {
infix operator ** : MultiplicationPrecedence
func **<T:BinaryInteger> (lhs: T, rhs: T) -> T { // BinaryInteger 프로토콜을 채택하는 타입만 들어올 수 있음 (아래와 같이 써도 된다.)
// func **<T> (lhs: T, rhs: T) -> T where T:BinaryInteger {
return lhs * rhs
}
print(3 ** 5) // 15
print(3.0 ** 5.0) //15
precedencegroup MyLeftOperator {
associativity: left
higherThan: LogicalConjunctionPrecedence // 상대적인 개념
}
infix operator ^^ : MyLeftOperator // 중위 연산자라는 의미 전위는 prefix, 후위는 postfix, 세개의 값 사이에 삽입된 두개의 연산자는 ternary)
func ^^ (lhs: String, rhs: Int) -> String { // 기호만 됨 (글자를 쓰면 함수가 된다. (그냥 기호로 된 함수라고 생각하자))
return "\(lhs)-\(rhs)"
}
let aiden = "Aiden" ^^ 5 // Aiden-5