Search code examples
swift

Swift Convert comma separated string to NSMutableArray


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]

Solution

  • Swift 2:

    Here you go:

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

    or

    var pointsArr = points.componentsSeparatedByString(",")
    

    Source: Split a String into an array in Swift?

    Swift 3:

    as Genki mentioned:

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