본문 바로가기

iOS/Swift

Function / print(_:separator:terminator:)

print를 할 때 separator, terminator를 이용하여 출력하는 방법!

func print(
    _ items: Any...,
    separator: String = " ",
    terminator: String = "\n"
)

Parameter의 종류는 다음과 같습니다.

items

     print할 아이템 입력

separator

    각각의 아이템들 사이에 출력할 문자열을 의미.

    기본값은 스페이스바(" ")입니다.

terminator

    모든 아이템들을 출력한 이후 출력할 문자열을 의미

    기본값은 개행("\n")입니다.

 

다음은 함수 사용의 예시입니당.

print("One two three four five")
// Prints "One two three four five"


print(1...5)
// Prints "1...5"


print(1.0, 2.0, 3.0, 4.0, 5.0)
// Prints "1.0 2.0 3.0 4.0 5.0"


print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"


for n in 1...5 {
    print(n, terminator: "")
}
// Prints "12345"

 


 

https://developer.apple.com/documentation/swift/print(_:separator:terminator:)

 

print(_:separator:terminator:) | Apple Developer Documentation

Writes the textual representations of the given items into the standard output.

developer.apple.com

 

'iOS > Swift' 카테고리의 다른 글

Metatype(.self, .Type, .Protocol)  (1) 2023.11.08
Swift Xcode 프로젝트명 바꾸기  (0) 2023.08.01
Swift Generic이란??  (0) 2023.07.13
Instance Method / prefix(_:), suffix(_:)  (0) 2023.06.06
Initializer / init(repeating:count:)  (0) 2023.06.06