Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Convert comma separated string to NSMutableArray [duplicate]

Tags:

swift

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

   var points = "4,5" 
   var selectedArray : NSMutableArray = [points]
like image 966
Jake Brdley Avatar asked Jun 07 '15 19:06

Jake Brdley


1 Answers

Swift 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 3:

as gomfucius mentioned:

var arr = points.components(separatedBy: ",")
like image 119
BVB Avatar answered Nov 16 '22 01:11

BVB