swift可选元组返回类型 官网
Swift里,函数可返回可选元组类型,意味着返回值可能为nil。 func getMinMax(numbers: [Int]) -> (min: Int, max: Int)? { if numbers.isEmpty { return nil }var currentMin = numbers[0] var currentMax = numbers[0]for number in numbers[1.. currentMax { currentMax = number } } return (currentMin, currentMax)} 调用此函数: if let bounds = getMinMax(numbers: []) { print("Min: \(bounds.min), Max: \(bounds.max)")} else { print("The array is empty.")} 使用可选元组返回类型时,调用函数后要先检查是否为nil。