简体   繁体   English

如何将 Int 数组转换为 String? Swift 中的数组

[英]How can I convert an Int array into a String? array in Swift

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?我怎么解决这个问题?

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?]或者,如果您希望 stringArray 的类型为[String?]

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

This form of the map statement is a method on the Array type.这种形式的 map 语句是 Array 类型上的一种方法。 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.如果您只需要将 int 值安装到自定义表视图单元格中,您可能应该将数组保留为 ints,并将值安装到 cellForRowAtIndexPath 方法中的单元格中。

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:编辑#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:如果您只想打印数组,最好将其保留为 Int 对象数组,然后简单地打印它们:

arr.forEach { print($0) }

Update: Swift 5更新:斯威夫特 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"]

You should use你应该使用

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

in order to present the value in the label as a String.以便将标签中的值显示为字符串。

使用以下代码就地执行此操作:

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

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.您可以尝试将类型转换为任何数据类型,如 Float 、 Double 、 Int8 等,这里 map 将从数组中获取每个元素并更改为提到的数据类型并将其分配回您的数组。

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.如果您正在寻找诸如 Python 的.join() 、Perl 的join() 、PHP 的implode() ,您可以使用.joined(separator: String) —— 但仅限于字符串值列表。

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:所以你仍然需要如上所示的.map来进行转换,但是一旦完成,它看起来更像你习惯的:

    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.您可以做的一件很酷的事情是在输出的最后一个元素之前添加“and”或“or”,以利用已经存在的.map来提供牛津逗号分隔的列表。 The .enumerated() method can help out by giving you an index on each element the map iterates through: .enumerated()方法可以通过为您提供地图遍历的每个元素的索引来提供帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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