Swift

[ Swift ] print(_:separator:terminator:)

Forest Yun 2023. 10. 4. 13:06
728x90

 

print(_:separator:terminator:)

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

์ฃผ์–ด์ง„ ์•„์ดํ…œ์˜ ํ…์ŠคํŠธ ํ‘œํ˜„์„ ํ‘œ์ค€ ์ถœ๋ ฅํ•œ๋‹ค. items๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๊ฐ’์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

 

Items

items์€ Any ํƒ€์ž…์œผ๋กœ, ํƒ€์ž…๊ณผ ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜์— ์ƒ๊ด€์—†์ด ํ…์ŠคํŠธ๋กœ ํ‘œํ˜„๋˜์–ด ์ถœ๋ ฅ๋œ๋‹ค. items๋Š” String(item)๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ž๋™์œผ๋กœ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž… ๋ณ€ํ™˜์„ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค

let number: Int = 120

print(number)
// Prints "120"

 

 

seperator

separator์€ ์—ฌ๋Ÿฌ ์•„์ดํ…œ์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ฐ ์•„์ดํ…œ ์‚ฌ์ด์— ์ถœ๋ ฅ๋˜๋Š” ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค. ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” " " ์ด ์„ค์ •๋˜์–ด ์žˆ๋‹ค.

let car = "์ฐจ"
let train = "๊ธฐ์ฐจ"
let number = 134

print(car, train, number, seperator: ".....")
// Prints "์ฐจ.....๊ธฐ์ฐจ.....134"

 

terminator

terminator์€ ๋ชจ๋“  ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋œ ํ›„ ๋งˆ์ง€๋ง‰์— ์–ด๋–ค ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•  ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค. ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ์ค„๋„๊ธฐ๋ฅผ ํ•˜๋Š” "\n"์ด ์ง€์ •๋˜์–ด ์žˆ๋‹ค.

for num in 0 ... 5 {
   print(num, terminator: "")
}

// Prints "012345"

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ฃผ์–ด์ง„ ๋ฌธ์ž๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ๋„์–ด์“ฐ๊ธฐ๋‚˜ ์ค„๋„๊ธฐ ์—†์ด ์ถœ๋ ฅํ•ด์•ผํ•  ๋•Œ + ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค String์— ๋ถ™์—ฌ์ค„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, print ๋ฉ”์„œ๋“œ์˜ terminator ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

for _ in 0 ..< a {
    print(s1, terminator: "")
}
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 

 

print(_:separator:terminator:)
728x90

'Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ Swift ] zip(_:_:)  (1) 2023.10.08
[ Swift ] isCase | isLowercase | isUppercase  (0) 2023.10.04
[ Swift ] ํ”„๋กœํ† ์ฝœ ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ  (0) 2023.09.24
[ Swift ] Serialization์ด๋ž€?  (0) 2023.01.18
[ Swift ] ์˜ต์…”๋„ Optionals  (0) 2021.09.05