You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func doSomething() {
// 비동기적으로 실행하는 클로저
// 해당 클로저는 오래동안 저장할 필요가 있음 ==> 새로운 스택을 만들어서 실행하기 때문
DispatchQueue.global().async {
print("나의 이름은 \\(self.name)입니다.")
}
}
}
var choco = Dog()
choco.doSomething()
클로저 캡처 리스트 - Strong Reference Cycle(강한 참조 순환) 해결
클로저는 기본적으로 캡처 현상이 발생
클로저와 인스턴스가 강한참조로 서로를 가르키고 있다면(Strong Reference Cycle),
메모리에서 정상적으로 해제되지 않고, 메모리 누수 현상이 발생
캡처리스트 내에서, 약한 참조 또는 비소유 참조를 선언해서 문제해결
class Person {
let name = "홍길동"
func sayMyName() {
print("나의 이름은 \\(name)입니다.")
}
func sayMyName1() {
DispatchQueue.global().async {
print("나의 이름은 \\(self.name)입니다.")
}
}
func sayMyName2() {
DispatchQueue.global().async { [weak self] in
print("나의 이름은 \\(self?.name)입니다.")
}
}
func sayMyName3() {
DispatchQueue.global().async { [weak self] in
guard let weakSelf = self else { return } // 가드문 처리 ==> 객체없으면 일종료
print("나의 이름은 \\(weakSelf.name)입니다.(가드문)")
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
클로져
함수를 1급객체(First-class Object)로 취급
클로저의 형태
클로저의 사용하는 이유?
클로저의 문법 최적화
트레일링(Trailing) 클로저
후행 클로저 문법
콜백 함수
멀티플 트레일링 클로저 → Swift 5.3
여러개의 함수를 파라미터로 사용할때
기존 방식에서는 마지막 클로저만 트레일링 클로저로 쓸 수 있었음
클로저의 메모리
클로저의 캡처
@escaping
함수의 파라미터 중 클로저 타입에 @escaping 키워드가 필요한 경우
@escaping 사용의 대표적인 경우
GCD 비동기 코드
@autoclosure 키워드
캡처 현상
변수를 캡처하는 함수(중첩 함수의 내부 함수) - 캡처 현상의 발생
캡처리스트의 형태
파라미터가 없는 경우
파라미터가 있는 경우
밸류(Value) 타입 캡처와 캡처리스트
참조(Reference) 타입 캡처와 캡처리스트
강한 참조 사이클 문제의 해결 - 캡처리스트 + weak/unowned
캡처리스트에서 바인딩하는 것도 가능
일반적인 클로저의 사용(객체 내에서의 사용, self키워드)
클로저 캡처 리스트 - Strong Reference Cycle(강한 참조 순환) 해결
Beta Was this translation helpful? Give feedback.
All reactions