brunch

You can make anything
by writing

C.S.Lewis

by Tilltue Nov 14. 2016

Swift3.0, @escaping closure

swift3.0, SE-0103

Swift 3.0 의 변경점중 SE-0103 에 대한 내용이다.

자세한 링크는 아래.


비동기 작업을 수행하는 함수가 있고, completion closure 를 인자로 받는다.

2.0 의 아래와 같은 형태로 가능했다.


class MyClass {

    func someAction(completion: () -> ()) {

        DispatchQueue.main.async {

            completion()

        }

    }

}


3.0 에서는 에러가 발생한다.

Closure use of non-escaping parameter 'completion' may allow it to escape

Fix it Parameter 'completion' is implicitly non-escaping


Swift 3.0 @escaping closure 의 경우에 명시를 해주도록 변경되었다. 

( @noescape 가 기본이다. )


escaping closure 는 closure 를 인자로 받는 함수에서, 해당 closure 가 함수가 반환된 이후에 수행되는 경우를 말한다.

아래는 샘플 코드이다.

class ClosureSample {

    func nonescapeSample(closure : () -> Void ) {

        closure()

    }

    func escapeSmaple(closure: @escaping () -> Void ) {

        DispatchQueue.main.async {

            closure()

        }

    }

}









작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari