Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将字符串数组描述转换为数组_Arrays_Swift - Fatal编程技术网

Arrays 将字符串数组描述转换为数组

Arrays 将字符串数组描述转换为数组,arrays,swift,Arrays,Swift,我有以下代码: var data = [12,33,22,44]; var a = data.description; // results is "[12,33,22,44]" var new = Array(a); // result is ['[','1'. not [12,33,22,44] 除了拆分“a”和迭代结果之外,还有一种最快的方法可以将字符串转换为数组吗 谢谢你可以 将[和]字符修剪为stringByTrimmingCharactersInSet 将其拆分为由逗号分隔

我有以下代码:

var data = [12,33,22,44];
var a =  data.description;  // results is "[12,33,22,44]"
var new = Array(a); // result is ['[','1'.  not [12,33,22,44]
除了拆分“a”和迭代结果之外,还有一种最快的方法可以将字符串转换为数组吗

谢谢

你可以

  • [
    ]
    字符修剪为
    stringByTrimmingCharactersInSet
  • 将其拆分为由逗号分隔的组件数组;及
  • 修剪每个项目周围的空白
例如:

let array = [12,33,22,44];
let string = array.description;  // result is "[12, 33, 22, 44]", not "[12,33,22,44]"

let results = string
    .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
    .componentsSeparatedByString(",")
    .map { return $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
或者,如果需要整数数组而不是字符串数组:

let results = string
    .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
    .componentsSeparatedByString(",")
    .map { return $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).toInt()! }  // remove `!` if you're not assured that only integers will be present
你可以

  • [
    ]
    字符修剪为
    stringByTrimmingCharactersInSet
  • 将其拆分为由逗号分隔的组件数组;及
  • 修剪每个项目周围的空白
例如:

let array = [12,33,22,44];
let string = array.description;  // result is "[12, 33, 22, 44]", not "[12,33,22,44]"

let results = string
    .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
    .componentsSeparatedByString(",")
    .map { return $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
或者,如果需要整数数组而不是字符串数组:

let results = string
    .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
    .componentsSeparatedByString(",")
    .map { return $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).toInt()! }  // remove `!` if you're not assured that only integers will be present

您可以通过拆分数组并过滤所有无法转换为
Int
的元素来实现这一点

 "[12,33,22,44]".componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "[,]")).filter{ $0.toInt() != nil }.map{ $0.toInt()! } // [12, 33, 22, 44]

您可以通过拆分数组并过滤所有无法转换为
Int
的元素来实现这一点

 "[12,33,22,44]".componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "[,]")).filter{ $0.toInt() != nil }.map{ $0.toInt()! } // [12, 33, 22, 44]

罗布对Swift 2.2的回答

let results = value
            .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
            .componentsSeparatedByString(",")
            .map { return Int($0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))!}  // remove `!` if you're not assured that only integers will be present

罗布对Swift 2.2的回答

let results = value
            .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
            .componentsSeparatedByString(",")
            .map { return Int($0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))!}  // remove `!` if you're not assured that only integers will be present

一种安全的方法是将字符串编码为数据,然后将数据解码为JSON,最后将结果向下转换为整数数组:

let base = [12,33,22,44]
let source = base.description
if let data = source.dataUsingEncoding(NSUTF8StringEncoding) {
    if let arrayOfInts = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [Int] {
        print(arrayOfInts) // [12, 33, 22, 44]
        print(arrayOfInts[1]) // 33
    }
}

一种安全的方法是将字符串编码为数据,然后将数据解码为JSON,最后将结果向下转换为整数数组:

let base = [12,33,22,44]
let source = base.description
if let data = source.dataUsingEncoding(NSUTF8StringEncoding) {
    if let arrayOfInts = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [Int] {
        print(arrayOfInts) // [12, 33, 22, 44]
        print(arrayOfInts[1]) // 33
    }
}
Swift 3代码:

let results = "[12,33,22,44]"
              .trimmingCharacters(in: CharacterSet(charactersIn: "[]"))
              .components(separatedBy:",")

print("Result array: \(results)")
Swift 3代码:

let results = "[12,33,22,44]"
              .trimmingCharacters(in: CharacterSet(charactersIn: "[]"))
              .components(separatedBy:",")

print("Result array: \(results)")

实际上变量
data
是一个数组。我想他想要一个只提供数组描述的方案。实际上变量
data
是一个数组。我想他想要一个只提供数组描述的方案。