我正在使用一系列年份,范圍為1950...2022。
這是我得到這個的代碼:
let years = (1950...2022).map() { String($0) }
但是我必須像2022...1950這樣按降序排列,如果我寫
let years = (2022...1950).map() { String($0) }
這給了我以下錯誤:
執行緒 1:致命錯誤:范圍需要 lowerBound <= upperBound
有誰知道如何按降序獲得它?
uj5u.com熱心網友回復:
你應該使用reversed
(1950...2022).reversed().map({ String($0) }
https://developer.apple.com/documentation/swift/array/1690025-reversed
或者你可以使用stride
stride(from: 2022, through: 1950, by: -1).map({ String($0) })
https://developer.apple.com/documentation/swift/1641347-stride
uj5u.com熱心網友回復:
采用
let arr = Array(stride(from: 2022, to: 1950, by: -1))
print(arr)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476590.html
