iOS Swift callAsFunction

사용 언어: Swift 5
사용 버전: Playgrounds 4.4.1

iOS 스위프트 callAsFunction을 알아보겠습니다.


callAsFunction은 struct를 함수처럼 부를 수 있게 해줍니다. 즉, 긴 함수 이름을 적을 필요 없이 바로 부를 수 있게 되어 편합니다.

공식 문서입니다.
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/declarations#Methods-with-Special-Names


예시로 이런 게 있네요.





Playground에서 한 번 테스트해 봅시다.
CallableSumTest라고 struct를 만든 다음에 callAsFunction을 정의해 줍니다.






짜잔!
1과 2를 더한 3이 나왔네요.

물론 callAsFunction을 직접 불러도 됩니다. 하지만 이럴 경우에는 callAsFunction을 쓸 이유가 없지요.







끝.


카테고리: iOS

댓글

이 블로그의 인기 게시물

Python urllib.parse.quote()

Python bytes.fromhex()

Python OpenCV 빈 화면 만들기

Android Notification with Full Screen

Android Minimum touch target size

Android Compose Instrumentation test to unit test

KiCad 시작하기 7 (FreeRoute 사용하기 2)

딩기 요트 명칭

tensorflow tf.expand_dims()

Android AVD Ram size change