Custom String Interpolation
โญ๏ธ ๆณจๆ๏ผString.StringInterpolation == DefaultStringInterpolation
่ฎ String interpolation ๅฏไปฅๆๅ ฅๆฐ็ๅๅฅๆ่ช่จๆๅ ฅๆนๅผ๏ผ
extend DefaultStringInterpolation
add an overload of appendInterpolation(_:)
๐ replit
let name = "Joe"
let age = 17
// โญ๏ธ String Interpolation:
// ------------------------
// 1. String
// 2. Int
// 3. struct User
// 4. class Person
// String ๆฌไพๅฐฑ็ฅ้ๅฆไฝๆๅ
ฅไธไบๅ
งๅปบ็ๅๅฅ
"My name is \(name), I'm \(age) years old"
// โฐโโ1โโโฏ โฐโ2โโโฏ
struct User {
var name: String
var age : Int
}
let joe = User(name: "Joe", age: 17)
// ๅฐฑ็ฎๆฏไธๅ struct๏ผไนๆฒๅ้กใ
"Hi, I'm \(joe)" // Hi, I'm User(name: "Joe", age: 17)
// โฐโ3โโโฏ
class Person {
var name: String = ""
var age : Int = 0
}
// class instance ไนๅฏไปฅ
let nobody = Person()
"Hi, I'm \(nobody)" // Hi, I'm Person
// โฐโโ 4 โโโฏ
// โญ๏ธ Custom String Interpolation
// -------------------------------
// ๅฆๆ่ฆๅฐ็นๅฎ็ๅๅฅๅๅฎข่ฃฝๅ็ String Interpolation๏ผ
// ๅฐฑๅฟ
้ ๆดๅ
DefaultStringInterpolationใ
// โญ๏ธ ๆณจๆ๏ผString.StringInterpolation == DefaultStringInterpolation
extension String.StringInterpolation {
mutating func appendInterpolation(_ user: User) {
appendInterpolation("My name is \(user.name) and I'm \(user.age) years old.")
}
}
"\(joe)" // My name is Joe and I'm 17 years old.
ๅฏๆงๅถๆธๅญ็ๅฐๆธไฝๆธใ
import Foundation // for String(format: "%.2f", n)
import SwiftUI // for CGFloat
extension DefaultStringInterpolation {
// โญ๏ธ `\(cgfloat, format: "%.2f")`
mutating func appendInterpolation(_ n: CGFloat, format: String) {
let result = String(format: "%.2f", n)
appendLiteral(result)
}
}
let n: CGFloat = 2.3456
"\(n, format: "%.2f")" // 2.35
import Foundation // for NumberFormatter, DateFormatter
extension DefaultStringInterpolation {
// โญ๏ธ "\(int, style: .spellOut)"
mutating func appendInterpolation(_ value: Int, style: NumberFormatter.Style) {
// create and configure NumberFormatter
let formatter = NumberFormatter()
formatter.numberStyle = style
// format the value
if let result = formatter.string(from: value as NSNumber) {
appendLiteral(result)
}
}
// โญ๏ธ "\(date, style: .short)"
mutating func appendInterpolation(_ value: Date, style: DateFormatter.Style) {
// create and configure DateFormatter
let formatter = DateFormatter()
formatter.dateStyle = .short
// format the value
let result = formatter.string(from: value)
appendLiteral(result)
}
// โญ๏ธ "\(date, format: "yyyy.MM.dd")"
mutating func appendInterpolation(_ value: Date, format: String) {
// create and configure DateFormatter
let formatter = DateFormatter()
formatter.dateFormat = format
// format the value
let result = formatter.string(from: value)
appendLiteral(result)
}
// โญ๏ธ "\(twitter: username)"
mutating func appendInterpolation(twitter: String) {
let link = "<a href=\"https://twitter.com/\(twitter)\">@\(twitter)</a>"
appendLiteral(link)
}
// โญ๏ธ "\(names, empty: "default value")"
mutating func appendInterpolation(
_ values: [String],
empty defaultValue: @autoclosure () -> String
) {
// โญ๏ธ why we need the "; return" part:
// https://stackoverflow.com/questions/31138273/swift-error-guard-body-must-not-fall-through
guard !values.isEmpty else { appendLiteral(defaultValue()); return }
appendLiteral(values.joined(separator: ", "))
}
}
// Int
let age = 38
"you are \(age)" // "you are 38"
"you are \(age, style: .spellOut)" // "you are ไธๅๅ
ซ"
// Date
let now = Date()
"Today's date is \(now)"
// "Today's date is 2020-08-16 10:17:03 +0000"
"Today's date is \(now, style: .short)"
// "Today's date is 2020/8/16"
"Today's date is \(now, format: "yyyy.MM.dd")"
// "Today's date is 2020.08.16"
"Twitter: \(twitter: "joe")"
// Twitter: <a href="https://twitter.com/joe">@joe</a>
// [String]
let none = [String]()
let names = ["Mike", "Jane", "Kelly"]
"Crew: \(none)"
"Crew: \(names, empty: "No one")." // "Crew: Mike, Jane, Kelly"
"Crew: \(none, empty: "No one")" // "Crew: No one"
Swift โฉ
Basic Behaviors โฉ
CustomStringConvertible (protocol) - for
String(describing:)
andprint(_:)
Last updated