让我们深入探讨“字符串插值(StringInterpolation)”,一个 Swift 5 里很酷的特性。
在“字符串插值”的帮助下,我们可以定义在打印对象时该对象在字符串中的显示方式(在使用 print 函数或使用 po
调试命令时)。
当我们打印 Struct 时,将会输出结构体名称,然后是其所有属性。代码如下:
struct AppUser{
var firstName: String
var lastName: String
}
let appUser = AppUser(firstName : "Payal", lastName : "Maniyar")
print("User details: \(appUser)")
// 输出:
User details: AppUser(firstName: "Payal", lastName: "Maniyar")
但是如果我们想打印类对象,那么行为与 Struct 不同了。代码片段如下:
class Point {
let x: Int, y: Int
init( x: Int, y: Int){
self.x = x
self.y = y
}
}
let p = Point(x: 21, y: 30)
print(p)
// 输出:
__lldb_expr_11.Point
要像 Struct 那样打印类对象,需要用到 CustomStringConvertible
协议,来看看这段代码里的用法:
class Point {
let x: Int, y: Int
init( x: Int, y: Int){
self.x = x
self.y = y
}
}
let p = Point(x: 21, y: 30)
print(p)
extension Point: CustomStringConvertible {
var description: String {
return "Point : (\(x), \(y))"
}
}
// 输出:
Point : (21, 30)
如果使用 CustomStringConvertible
,则可以实现 description
属性来打印对象。
通过字符串插值还可以使用不同的参数和结果来打印对象。来理解一下这段代码:
class User {
var firstName: String
var lastName: String
init (firstName: String,lastName: String){
self.firstName = firstName
self.lastName = lastName
}
}
//StringInterpolation 1
extension String.StringInterpolation{
mutating func appendInterpolation(_ user: User) {
appendInterpolation("My name is : \(user.firstName) \(user.lastName)")
}
}
//StringInterpolation 2
extension String.StringInterpolation{
mutating func appendInterpolation(_ firstName: String,_ lastName: String) {
appendInterpolation("First Name : \(firstName) Last Name : \(lastName)")
}
}
let user = User(firstName : "Payal ", lastName : "Maniyar")
print("User details: \(user.firstName,user.lastName)")// This will use StringInterpolation 2
print("User details: \(user)")// This will use StringInterpolation 1
// 输出为:
User details: First Name : Payal Last Name : Maniyar
User details: My name is : Payal Maniyar
在上面的代码中,通过参数可以选择哪一个 StringInterpolation
扩展被使用。
继续来使用更高级的用法……
你的自定义插值方法可以根据需要获取任意多的参数,无论是否带标签。例如,我们可以使用各种样式添加插值以打印数字,如下所示:
import Foundation
extension String.StringInterpolation {
mutating func appendInterpolation(_ number: Int, style: NumberFormatter.Style) {
let formatter = NumberFormatter()
formatter.numberStyle = style
if let result = formatter.string(from: number as NSNumber) {
appendLiteral(result)
}
}
}
let number = Int.random(in: 0...100)
let lucky1 = "The lucky number this week is \(number, style: .ordinal)."
print(lucky1)
// 输出为:
The lucky number this week is 100th.
你可以根据需要多次调用 appendLiteral()
,也可以根本不调用。例如,我们可以添加一个字符串插值来多次重复一个字符串:
extension String.StringInterpolation {
mutating func appendInterpolation(repeat str: String, _ count: Int) {
for _ in 0 ..< count {
appendLiteral(str)
}
}
}
print("\(repeat: "Do what you like\n ", 6)")
// 输出:
Do what you like
Do what you like
Do what you like
Do what you like
Do what you like
Do what you like
而且,由于这些只是常规方法,你可以使用 Swift 的全部功能。例如,我们可能会添加一个将字符串数组连接起来的插值,但若这个数组为空,则执行另一个返回字符串的闭包:
extension String.StringInterpolation {
mutating func appendInterpolation(_ values: [String], empty defaultValue: @autoclosure () -> String) {
if values.count == 0 {
appendLiteral(defaultValue())
} else {
appendLiteral(values.joined(separator: ", "))
}
}
}
func EmptyMsg () -> String {
return "No one found"
}
var names = ["Harry", "Ron", "Hermione"]
print("List of students: \(names, empty: EmptyMsg()).")
names.removeAll()
print("List of students: \(names, empty: EmptyMsg() ).")
// 输出:
List of students: Harry, Ron, Hermione.
List of students: No one found.
就是这样……
希望现在你已经清楚 StringInterpolation
的用法了。
原文: https://medium.com/@payalmaniyar/swift-5-understanding-of-string-interpolation-cdc8bb622ce3
作者:Payal Maniyar
编译:码王爷