bla*_*acx 1 string string-interpolation swift
当我需要根据某些条件(在这种情况下是最喜欢的)构造一个字符串时,我经常让自己编写这样的代码:
let me = Contact(name: "Stefan", isFavorite: true)
var message = "Contact \(me.name)"
if me.isFavorite {
message.append(" is a favorite contact")
}
Run Code Online (Sandbox Code Playgroud)
对于这样一个简单的任务,这些是 4 行或一个复杂的三元运算符(if ? then : else)。对此,我总是心存芥蒂……
有没有办法用 Swift 更优雅地做到这一点?
确实有 - 我在 UIKonf 2019 上找到了答案,在那里我从 Erica Sadun 那里听说有一种方法Swift 5 String Interpolation可以在一行中实现这一点。您只需要这个可重用的扩展:
extension String.StringInterpolation {
mutating func appendInterpolation(if condition: @autoclosure () -> Bool, _ literal: StringLiteralType) {
guard condition() else { return }
appendLiteral(literal)
}
}
Run Code Online (Sandbox Code Playgroud)
它使您能够将 4 行转换为一行:
"Contact: \(me.name)\(if: me.isFavorite, " is a favorite contact")"
Run Code Online (Sandbox Code Playgroud)
您可以在 Erica 的文章中了解 Swift 字符串插值的不同可能性:https : //ericasadun.com/2018/12/12/the-beauty-of-swift-5-string-interpolation/
开始弄脏你的手:
import Foundation
// *****************************************************************************
// Many thanks to Erica Sadun for describing the new possibilities of string
// interpolation in Swift 5
// *****************************************************************************
// *****************************************************************************
// Conditional Interpolation
struct Contact {
let name: String
let isFavorite: Bool
}
let me = Contact(name: "Stefan", isFavorite: true)
// ***************************************************************** Swift 4
var message = "Contact \(me.name)"
if me.isFavorite {
message.append(" is favorite")
}
message // we need 4 lines to construct this message!!!
"Contact: \(me.name)\(me.isFavorite ? " is favorite" : "")" // or a complex ternary operator
// ***************************************************************** Swift 5
extension String.StringInterpolation {
mutating func appendInterpolation(if condition: @autoclosure () -> Bool, _ literal: StringLiteralType) {
guard condition() else { return }
appendLiteral(literal)
}
}
"Contact: \(me.name)\(if: me.isFavorite, " is favorite")" // simple and clean - no extras
// *****************************************************************************
// Optional Interpolation
let optionalMe: Contact? = Contact(name: "Stefan", isFavorite: true)
// ***************************************************************** Swift 4
"Contact: \(optionalMe?.name)" // shows warning
// ***************************************************************** Swift 5
// Interpolate nil value
extension String.StringInterpolation {
/// Provides `Optional` string interpolation without forcing the
/// use of `String(describing:)`.
public mutating func appendInterpolation<T>(_ value: T?, default defaultValue: String) {
if let value = value {
appendInterpolation(value)
} else {
appendLiteral(defaultValue)
}
}
}
let nilContact: Contact? = nil
"Contact: \(nilContact?.name, default: "nil")"
// Strip `Optional`
extension String.StringInterpolation {
/// Interpolates an optional using "stripped" interpolation, omitting
/// the word "Optional" from both `.some` and `.none` cases
public mutating func appendInterpolation<T>(describing value: T?) {
if let value = value {
appendInterpolation(value)
} else {
appendLiteral("nil")
}
}
}
"Contact: \(describing: optionalMe?.name)"
Run Code Online (Sandbox Code Playgroud)
您可以使用三元运算符
let msg = "Contact \(me.name)" + (me.isFavorite ? " is a favorite contact" : "")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
513 次 |
| 最近记录: |