iOS Use a function as a parameter

운영 체제: macOS Monterey 12.3.1
사용 버전: Xcode 13.3.1, SwiftUI

iOS Use a function as a parameter를 알아보겠습니다.


Swift에서는 파라미터로 함수를 넣을 수 있습니다.

어떤 건지 SwiftUI Button으로 한 번 예를 들어볼게요.

SwiftUI 문서를 보면, Button을 처음 정의할 때 다음과 같은 것이 필요합니다.






위의 문서를 보면, action: () -> Void, lable: () -> Label이 보입니다. 이 뜻은 action과 label에 함수가 필요하다는 뜻입니다.


아래처럼 Button에 소괄호를 열고 바로 파라미터를 넣을 수 있습니다.






다른 방식으로는 중괄호를 연속적으로 열어서 처리할 수 있습니다.

첫 번째 중괄호가 action을 의미합니다.






label에 Text()가 들어가는 이유는 반환값이 Label로 존재하기 때문입니다.



끝.



카테고리: iOS

댓글

이 블로그의 인기 게시물

Python urllib.parse.quote()

KiCad 시작하기 4 (기존 회로도 수정 및 추가)

Android Compose Coil library

KiCad 시작하기 1 (회로도 만들기)

Android Notification with Full Screen

iOS Swift callAsFunction

Android Custom IME(Input method editor) 만들기

iOS Error Undefined symbol Testing.Trait

Android Compose automation for getting localized images to use on Play Store app image

tensorflow tf.expand_dims()