CustomStringConvertible
인스턴스를 문자열로 변환할 때 커스텀하게 변환할수 있는 프로토콜이다.
struct Point {
let x: Int, y: Int
}
let p = Point(x: 21, y: 30)
print(p)
// Prints "Point(x: 21, y: 30)"
기본 Point 구조체를 출력할때 기본 출력값으로 나오지만
CustomStringConvertible 를 사용하면 아래 description을 준수해야 하며
extension Point: CustomStringConvertible {
var description: String {
return "(\(x), \(y))"
}
}
print(p)
// Prints "(21, 30)"
원하는 출력값으로 변형하여 출력을 가능하게 한다.
참고 :
https://developer.apple.com/documentation/swift/customstringconvertible
'IOS🍎 > iOS+Swift' 카테고리의 다른 글
[iOS] Swift Unit Test 살펴보기 (0) | 2022.04.07 |
---|---|
[Swift] Access Control (open, public, internal, fileprivate, private) (0) | 2022.03.29 |
[iOS/swift] 유용한 사이트 정리 모음.zip (0) | 2021.07.03 |
[iOS] Codable - Encodable & Decodable 프로토콜 (0) | 2021.05.31 |
[iOS] View Controller 생명주기 (0) | 2021.05.03 |
댓글