在多参数函数中解包可选

Unwrap an Optional Within a Multiple Parameter Function

这个问题可能看起来很基础,但我正在尝试解开下面代码中的 middleName 可选参数。我已经在这里成功解包了可选的 return 但我仍然收到 Matthew Optional("Matt") Stevenson 记录。如何打开 "Matt" 并删除可选的 return?

func returnFullName (name: (firstName: String, middleName: String?,
                    lastName: String)) -> String? 
    {
    return ("\(name.firstName) \(name.middleName) \(name.lastName)")
}

var fullName = returnFullName(("Matthew", middleName: "Matt", lastName: "Stevenson"))

if let printFullName = fullName {
    print (printFullName)
}

您可以使用 nil coalescing operator 作为可选的 middleName 属性。如果值为 nil,将打印默认值 "",而如果值为 non-nil 字符串,它将被展开。

return ("\(name.firstName) \(name.middleName ?? "") \(name.lastName)")

但是请注意,您不需要让 return 类型的 returnFullName(..) 函数成为可选的,因为该值永远不会为零。

func returnFullName (name: (firstName: String, middleName: String?,
    lastName: String)) -> String {
    return ("\(name.firstName) \(name.middleName ?? "") \(name.lastName)")
}

print(returnFullName(("Matthew", middleName: "Matt", lastName: "Stevenson"))) 
    // Matthew Matt Stevenson
print(returnFullName(("Matthew", middleName: nil, lastName: "Stevenson")))    
    // Matthew  Stevenson   <-- extra unwanted space 

此外,我没有看到任何明显的理由在函数签名中使用元组,而不是为不同的名称组件使用单独的参数。因此,替代方案如下:

func returnFullName (firstName: String, middleName: String?,
    lastName: String) -> String {
    return "\(firstName) \(middleName ?? "") \(lastName)"
}

print(returnFullName("Matthew", middleName: "Matt", lastName: "Stevenson")) 
    // Matthew Matt Stevenson
print(returnFullName("Matthew", middleName: nil, lastName: "Stevenson"))    
    // Matthew  Stevenson   <-- extra unwanted space

最后,为了避免 nilmiddleName 的额外 space,我建议保留 single-line return 而不是使用带有 if let - else 子句的可选绑定:

func returnFullName(firstName: String, middleName: String?, lastName: String) -> String {
    if let middleName = middleName {
        return ("\(firstName) \(middleName) \(lastName)")
    }
    else {
        return ("\(firstName) \(lastName)")
    }
}

print(returnFullName("Matthew", middleName: "Matt", lastName: "Stevenson"))
    // Matthew Matt Stevenson
print(returnFullName("Matthew", middleName: nil, lastName: "Stevenson"))
    // Matthew Stevenson

现在,问题涉及为给定的示例展开一个可选变量,现在是上面的 well-covered。然而,我想我会提到,您可以使用闭包和 Swifts in-built 函数式方法来压缩您的解决方案,如下所示:

// instead of a function: make use of a closure
let returnFullName : (String, String?, String) -> String = {
    [[=14=], , ].flatMap{ [=14=] }.joinWithSeparator(" ") }

print(returnFullName("Matthew", "Matt", "Stevenson"))
    // Matthew Matt Stevenson
print(returnFullName("Matthew", nil, "Stevenson"))
    // Matthew Stevenson

与其使用元组,不如传入 firstName、middleName 和 lastName 参数。在这种特殊情况下,元组似乎是不必要的。

    func returnFullName(firstName: String, middleName: String?, lastName: String) -> String?
    {
        if let middleName = middleName {
            return ("\(firstName) \(middleName) \(lastName)")
        } else {
            return ("\(firstName) \(lastName)")
        }
    }

let fullName = returnFullName("Matthew", middleName: "Matt", lastName: "Stevenson")

if let printFullName = fullName {
    print(printFullName)
}