简体   繁体   English

如何将[AnyObject]数组快速转换为字符串

[英]How to convert the [AnyObject] Array to string in swift

For example: Array like ["A12", 1, 2, "Test"]. 例如:数组,如[“ A12”,1,2,“ Test”]。 My expected result should be able to bind in the textfield or else to get as string with component seperator (,) 我的预期结果应该能够绑定到文本字段中,或者使用组件分隔符(,)作为字符串获取

First map the values to String values, and then just join them using separator that you like: 首先将值映射为String值,然后使用所需的分隔符将它们连接起来:

let description: String = ["A12", 1, 2, "Test"].map{ String(describing: $0) }.joined(separator: ", ")
print(description)

You can get it done with this, 你可以做到这一点,

    let array: [Any] = ["A12", 1, 2, "Test"]
    let tmpArray = array.map({ return String(describing: $0)})
    let string = tmpArray.joined(separator: ",")
    print(string)

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

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