学校排行榜

(0)

投一票

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。
专业
评论
新闻
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。