Closures - The Swift Programming Language (Swift 5.6)
클로저는 C / Objective-C 의 blocks 및 lambdas 와 유사함
정의된 컨텍스트에서 모든 상수와 변수에 대한 참조를 캡처하고 저장할 수 있음
상수와 변수를 닫는 것 {}
Capture의 모든 메모리 관리를 처리
컨텍스트에서 파라미터와 반환값 타입 유추
단일 표현식 클로저의 암시적 반환
Shorthand argument names
Trailing closure syntax
shorter versions of function 작성하는 것과 완전한 선언과 이름이 없는 함수에 유용
함수를 하나 이상의 인자로 사용하는 함수로 작업할 때 특히 유용
정렬 클로저의 출력을 기반으로 알려진 타입의 배열을 정렬하는 sorted(by:) 메서드
정렬 프로세스가 완료되면 메서드는 원본 개열과 같은 타입/크기로 정렬된 새로운 배열 반환
기존 배열은 sorted(by:)로 수정되지 않음
sorted(by:) 는 배열 내용과 동일한 타입의 두인자를 사용하는 클로저를 허용
값이 정렬된 후 첫 번째 값이 두 번째 값의 앞 또는 뒤에 표시되어야 하는지 여부를 나타내는 Bool값 반환