Swift条件字符串内插

时间:2019-05-30 15:37:55

标签: swift string string-interpolation

当我需要根据某些条件(在这种情况下为isFavorite)构造一个字符串时,我经常陷入这样的代码编写中:

let me = Contact(name: "Stefan", isFavorite: true)

var message = "Contact \(me.name)"
if me.isFavorite {
    message.append(" is a favorite contact")
}

对于这样一个简单的任务,它们是4行,或者是一个复杂的三元运算符(如果?,则为:else)。我对此总是有一个不好的良心...

有没有办法让Swift更优雅地做到这一点?

3 个答案:

答案 0 :(得分:4)

雨燕5

的确是-我在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)
    }
}

它使您可以将4行转换为一条:

"Contact: \(me.name)\(if: me.isFavorite, " is a favorite contact")"

您可以在Erica的文章中https://ericasadun.com/2018/12/12/the-beauty-of-swift-5-string-interpolation/

阅读有关Swift字符串内插的不同可能性的信息。

游乐场

开始用这种方法弄脏你的手

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)"

答案 1 :(得分:1)

您可以使用Ternary operator

let msg = "Contact \(me.name)" + (me.isFavorite ? " is a favorite contact" : "")

答案 2 :(得分:0)

我倾向于创建第二个String变量,该变量可以为空也可以不为空,并无条件地附加它...

let me = Contact(name: "Stefan", isFavorite: true)
let favorite = me.isFavorite ? " is a favorite contact" : ""

var message = "Contact \(me.name)\(favorite)"