This question already has an answer here:
这个问题在这里已有答案:
- Swift: Split a String into an array 29 answers
- Swift:将一个String拆分成一个数组29个答案
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: ",")