简体   繁体   中英

How to convert Range Int Array to String Array in Swift efficiently?

I create a range of in from 1-100 doing this:

let array = [Int](1...10) 

Now how do I convert that into a String array as I need it for UIPickerView

Thanks in advance.

 var arr = [1,2,3]

 let strArr = arr.map{String($0)}

OR in picker you may return the component title like this

 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return  "\(arr[row])"
 }

您可以直接将Int范围mapString数组

let stringArray = (1...10).map(String.init)

If it is just a sequence of strings "1", "2", .. you can simply get it using map :

let model = array.map { "\($0)" }

If you are looking for spelled out numbers, such as "one", "two", ..., use number formatter:

let numberFormatter: NumberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.spellOut
let model = array.map { numberFormatter.string(from: NSNumber(value: $0)) ?? "" }

好吧,你可以这样做:

let formattedArray = ([0,1,1,0].map{String($0)}).joined(separator: ",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM