문제
내 정답 코드
import Foundation
let n = readLine()!.components(separatedBy: " ").map { Int($0)! }
let (a, b) = (n[0], n[1])
var string = "a = \(a)\nb = \(b)"
print(string)
#
띄워쓰기라는 separator을 파라미터로 준 components를 사용하여 배열로 반환하였는데 그 과정에서 map을 통해 Int값으로 형변환을 해주었다.
배운 기술
0. print
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
1. components(separatedBy: )
https://developer.apple.com/documentation/foundation/nsstring/1413214-components
components(separatedBy:) | Apple Developer Documentation
Returns an array containing substrings from the receiver that have been divided by a given separator.
developer.apple.com
(+separator 구분자)
2. map { }
https://developer.apple.com/documentation/swift/array/map(_:)-87c4d
map(_:) | Apple Developer Documentation
Returns an array containing the results of mapping the given closure over the sequence’s elements.
developer.apple.com
'프로그래머스 (Swift) > 기초' 카테고리의 다른 글
[Day2-1] 덧셈식 출력하기 (0) | 2024.01.14 |
---|---|
[Day1-5] 특수문자 출력하기 (0) | 2024.01.14 |
[Day2-4] 홀짝 구분하기 (0) | 2024.01.14 |
[Day2-3] 문자열 돌리기 (0) | 2024.01.14 |
[Day1-1] 문자열 출력하기 (0) | 2024.01.08 |