-
Swift) swift 5에 추가된 String InterpolationSwift 2020. 11. 4. 00:06
출력에 대한 문자열 Interpolation은 단순히 placeholder를 통해 문자열 안에 문자열이 아닌 자료형을 넣어 표현 하거나, extension을 통해 CustomStringConvertible 프로토콜을 채택하여 수정한 후 출력 하였다.
import Foundation struct Identity { var name: String var age: Int var phoneNumber: Int } var id = Identity(name: "UY", age: 20, phoneNumber: 01012341234) extension Identity: CustomStringConvertible { var description: String { return "Name is \(name), and \(age) years old. Please call \(phoneNumber)" } } print("Name is \(id.name), and \(id.age) years old. Please call \(id.phoneNumber)") print(id) print("\(id)") //Name is UY and 20 years old. Please call 1012341234 //Name is UY and 20 years old. Please call 1012341234 //Name is UY and 20 years old. Please call 1012341234
일반 문자열 보간으로 Identity 구조체의 프로퍼티를 이용한 출력과 CustomStringConvertible 프로토콜을 채택한 description을 수정한 출력이다.
swift5에서 새롭게 추가된 StringInterpolation
StringInterpolation은 String 구조체의 구조체이다.
즉 문자열 보간에서 사용 가능한 타입이다.
기존 문자열을 출력 할때 자료형의 format을 다르게 출력하고 싶다면 어떻게 할까?
swift 5 이상에서 extension을 통해 String.StringInterpolation에 새로운 custom Interpolation을 추가 하여 출력시 해당 자료형에 대한 format을 다르게 정할 수 있다.
기본적인 Custom StringInterpolation 사용법 부터 보자.
extension String.StringInterpolation { mutating func appendInterpolation(_ value: Identity) { appendInterpolation("My name is \(value.name) and my phone number is \(value.phoneNumber)") } } print(id) print("\(id)") //Name is UY and 20 years old. Please call 1012341234 //My name is UY and my phone number is 1012341234
CustomStringConvertible로 출력방식을 수정한 생성자 id를 출력 할 때와 Interpolation을 사용한 출력 방식이 달라졌다.
파라미터를 전달하는 Interpolation
swift 5.0 이상에 새로 추가된 내용중 인상적인 것은 매개변수를 전달하는 Interpolation이다.
매개변수로써 다양한 데이터 타입을 전달 할 수 있고, 자유롭게 customize 할 수 있다.
extension String.StringInterpolation { mutating func appendInterpolation(parametter: String) { appendInterpolation("passed \(parametter)") } } print("\(parametter: "param ")") //passed param
extension String.StringInterpolation { mutating func appendInterpolation(value: String, operatorr: String) { appendInterpolation("\(value) \(operatorr) \(value)") } } print("\(value: "3", operatorr: "X")") // 3 X 3
extension String.StringInterpolation { mutating func appendInterpolation(_ data: Size, calOperator: String) { let size = Int(data.width) * Int(data.height) appendInterpolation("\(data.width) \(calOperator) \(data.height) = \(size)" ) } } struct Size { var width: Int var height: Int } var s = Size(width: 3, height: 5) print("\(s,calOperator: "X")") // 3 X 5 = 15
extension String.StringInterpolation { mutating func appendInterpolation(_ data: Size, style: NumberFormatter.Style) { let size = Int(data.width) * Int(data.height) let formatter = NumberFormatter() formatter.numberStyle = style if let width = formatter.string(for: data.width) , let height = formatter.string(for: data.height), let result = formatter.string(for: size) { appendInterpolation("\(width) X \(height) = \(result)") } } } struct Size { var width: Int var height: Int } var s = Size(width: 3, height: 5) print("\(s, style: .spellOut)") // three X five = fifteen
'Swift' 카테고리의 다른 글
Swift) API Design Guidelines (0) 2021.06.10 Swift) 함수 타입 (0) 2020.11.18 Swift) Optional (0) 2020.11.11 Swift) Type Inference과 컴파일 시간 (0) 2020.10.21 Swift) removeLast()와 popLast() (0) 2020.08.10