2

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.

4 Answers 4

4

You can directly map an Int range to a String array

let stringArray = (1...10).map(String.init)
Sign up to request clarification or add additional context in comments.

Comments

1

Well you can do it like :

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

Comments

0
 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])"
 }

2 Comments

When I do that I get this: {"Cannot use instance member 'arr' within property initializer; property initializers run before 'self' is available"} When put change the variables to "lazy var arr" it wont work with the UIPickerView
You need to call map inside viewDidLoad
0

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)) ?? "" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.