53

I have an array that looks like this:

var arr: [Int] = [1,2,3,4,5]

In order to print this, I would like to convert this to:

var newArr: [String?] = ["1","2","3","4","5"]

How can I solve this problem?

3
  • 9
    arr.map { String($0) }. If you really want the strings to be optional, arr.map { Optional(String($0)) } Commented Jun 23, 2015 at 13:22
  • Should I put something like: var newArr = arr.map {Optional(String($0)) }? @AirspeedVelocity Commented Jun 23, 2015 at 13:25
  • Yes. But, rereading, it’s not quite clear what you want…. when you say, “in order to print this”, what do you mean? Commented Jun 23, 2015 at 13:26

6 Answers 6

90

Airspeed Velocity gave you the answer:

var arr: [Int] = [1,2,3,4,5]

var stringArray = arr.map { String($0) }

Or if you want your stringArray to be of type [String?]

var stringArray = arr.map  { Optional(String($0)) }

This form of the map statement is a method on the Array type. It performs the closure you provide on every element in the array, and assembles the results of all those calls into a new array. It maps one array into a result array. The closure you pass in should return an object of the type of the objects in your output array.

We could write it in longer form:

var stringArray = arr.map {
  (number: Int) -> String in
  return String(number)
}

EDIT:

If you just need to install your int values into custom table view cells, you probably should leave the array as ints and just install the values into your cells in your cellForRowAtIndexPath method.

func tableView(tableView: UITableView, 
  cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("cell", 
    forIndexPath: indexPath) as! MyCustomCellType
  cell.textLabel?.text = "\(arr[indexPath.row])"
  return cell
}

Edit #2:

If all you want to to is print the array, you'd be better off leaving it as an array of Int objects, and simply printing them:

arr.forEach { print($0) }
Sign up to request clarification or add additional context in comments.

Comments

11

Update: Swift 5

You can do it using following code:

var arr: [Int] = [1,2,3,4,5]
let stringArray = arr.map(String.init)

output:

["1", "2", "3", "4", "5"]

Comments

2

You should use

 cell.textLabel?.text = "\(arr[indexPath.row])"

in order to present the value in the label as a String.

1 Comment

Qustion is not that, isn't it
2

Do it in place using the following piece of code:

Array(0...100).map { String($0) }

Comments

2

If you're looking do something like Python's .join(), Perl's join(), PHP's implode(), you can use .joined(separator: String) -- but only on a list of String values.

So you'll still need the .map as shown above to do the conversion, but once that's done, it looks a lot more like what you're used to:

    let stringifiedNumberList = numberList
        .map { String($0) }
        .joined(separator: ", ")

If you want to get fancy...

One cool thing you can do is add "and" or "or" before the last element in the output, to provide an Oxford comma delimited list, by taking advantage of the .map already being there. The .enumerated() method can help out by giving you an index on each element the map iterates through:

    let stringifiedNumberList = numberList
        .enumerated()
        .map {
            if $0.0 == (numberList.count - 1) {
                return "and " + String($0.1)
            } else {
                return String($0.1)
            }
        }
        .joined(separator: ", ")

Comments

1

Declare your integer array like this :

let integerArray = [1 , 2 ,4 ,5]

Print your typecasted array Like this :

print(integerArray.map { String($0) })

And you can try typecasting to any data type like Float , Double , Int8 etc , here map will take each element from array and change to mentioned data type and assign it back to your array.

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.