[Swift] init(repeating:count:)

김동준·2022년 4월 25일
0

함수들

목록 보기
1/7

[Swift] init(repeating:count:)

문자열 반복과, 배열 크기를 지정할 수 있는 함수이다.

init(repeating repeatedValue: String, count: Int)
let s = String(repeating: "ab", count: 10)
print(s)
// Prints "abababababababababab"

String값을 count수만큼 문자열로 반복시킬 수 있다.

또한,

let fiveZs = Array(repeating: "Z", count: 5)
print(fiveZs)
// Prints "["Z", "Z", "Z", "Z", "Z"]"

위와 같이 count크기의 배열을 선언 후 Character로 모든 요소를 채울 수도 있다.

profile
끊임없이 성장하는 예비개발자입니다.

0개의 댓글