15

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]
Jake Brdley
  • 173
  • 1
  • 1
  • 4

1 Answers1

40

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: ",")
Caleb Adams
  • 4,445
  • 1
  • 26
  • 26
BVB
  • 5,380
  • 8
  • 41
  • 62