반응형
매개변수에 클로저를 사용하는 경우
: 매개변수에 클로저를 사용하는 기본적인 방법
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 매개변수에 들어가는 클로저 | |
// completion이라는 클로저를 매개변수로 가지는 메소드 정의 | |
func sayHi(completion: () -> Void) { | |
print("sayHi() called") | |
sleep(2) // 2초 멈추기 | |
// compleition 클로저 실행 | |
completion() | |
} | |
// completion이라는 클로저를 매개변수로 가지는 메소드 호출 | |
// 메소드 호출부에서 이벤트 종료를 알 수 있다. | |
sayHi(completion: { | |
print("2초가 지났다.") | |
}) | |
결과 | |
sayHi() called | |
2초가 지났다. << 2초후에 실행됨 | |
데이터를 여러개 반환하는 클로저 & 클로저 함수 호출 단순화
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 매개변수로서 데이터를 반환하는 클로저 | |
func sayHiWithName(completion: (String) -> Void) { | |
print("sayHiWithName() called") | |
sleep(2) // 2초 멈추기 | |
// 클러조를 실행과 동시에 데이터 반환 | |
completion("클로저 실행") | |
} | |
// 동작은 같지만 단순화 할 수 있다 예제 - 1 | |
sayHiWithName(completion: { (comment: String) in | |
print("2초후 실행되었다 : \(comment)") | |
}) | |
// 동작은 같지만 단순화 할 수 있다 예제 - 2 | |
sayHiWithName(completion: { comment in | |
print("2초후 실행되었다 : \(comment)") | |
}) | |
// 동작은 같지만 단순화 할 수 있다 예제 - 3 | |
sayHiWithName { comment in | |
print("2초후 실행되었다 : \(comment)") | |
} | |
결과 | |
sayHiWithName() called | |
2초후 실행되었다 : 클로저 실행 | |
sayHiWithName() called | |
2초후 실행되었다 : 클로저 실행 | |
sayHiWithName() called | |
2초후 실행되었다 : 클로저 실행 | |
// 매개변수로서 데이터를 여러개 반환하는 클로저 | |
func sayHiWithFullName(completion: (String, String) -> Void) { | |
print("sayHiWithName() called") | |
sleep(2) // 2초 멈추기 | |
// 클러조를 실행과 동시에 데이터 반환 | |
completion("클로저 실행1","클로저 실행2") | |
} | |
// 동작은 같지만 단순화 할 수 있다 예제 - 1 | |
sayHiWithFullName { first, second in | |
print("첫번째 : \(first)") | |
print("두번째 : \(second)") | |
} | |
// 동작은 같지만 단순화 할 수 있다 예제 - 2 | |
sayHiWithFullName { | |
print("첫번째 : \($0)") | |
print("두번째 : \($1)") | |
} | |
결과 | |
sayHiWithName() called | |
첫번째 : 클로저 실행1 | |
두번째 : 클로저 실행2 | |
sayHiWithName() called | |
첫번째 : 클로저 실행1 | |
두번째 : 클로저 실행2 |
클로저의 옵셔널
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 클로저를 nill로 처리할 수 있다. | |
func sayHiOptional(completion: (() -> Void)? = nil) { | |
print("sayHiOptional() called") | |
sleep(2) // 2초 멈추기 | |
// compleition 클로저 실행 | |
completion?() | |
} | |
// 클로저가 nill이면 다음과 같이 호출할 수 있다. | |
sayHiOptional() | |
// 클로저가 nill이 아니면 다음과 같이 호출할 수 있다. | |
sayHiOptional { | |
} |
반응형
'iOS > Swift문법' 카테고리의 다른 글
[Swift 문법] 딕셔너리(Dictionary) - day8 (0) | 2022.03.15 |
---|---|
[Swift 문법] 상속 - day7 (0) | 2022.03.14 |
[Swift 문법] 클로저 - day5 (0) | 2022.03.10 |
[Swift 문법] 프로퍼티 옵저버 - day4 (0) | 2022.03.08 |
[Swift 문법] class와 struct - day3 (0) | 2022.03.07 |