Swift将逗号分隔的字符串转换为NSMutableArray [duplicate]

时间:2022-09-02 00:21:32

This question already has an answer here:

这个问题在这里已有答案:

I have the following string

我有以下字符串

var points = "4,5"

I would like to convert to a mutable array so it becomes [4,5] I tried the following but it did not work

我想转换为一个可变数组,所以它成为[4,5]我尝试了以下但它没有工作

   var points = "4,5" 
   var selectedArray : NSMutableArray = [points]

1 个解决方案

#1


25  

Swift 2:

斯威夫特2:

Here you go:

干得好:

var points = "4,5"
var pointsArr = split(points) {$0 == ","}

or

要么

var pointsArr = points.componentsSeparatedByString(",")

Source: Swift: Split a String into an array

来源:Swift:将String拆分为数组

Swift 3:

斯威夫特3:

as gomfucius mentioned:

正如gomfucius所说:

var arr = points.components(separatedBy: ",")

#1


25  

Swift 2:

斯威夫特2:

Here you go:

干得好:

var points = "4,5"
var pointsArr = split(points) {$0 == ","}

or

要么

var pointsArr = points.componentsSeparatedByString(",")

Source: Swift: Split a String into an array

来源:Swift:将String拆分为数组

Swift 3:

斯威夫特3:

as gomfucius mentioned:

正如gomfucius所说:

var arr = points.components(separatedBy: ",")