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]
0

1 Answer 1

43

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: ",")
Sign up to request clarification or add additional context in comments.

1 Comment

Swift 3: var pointsArr = points.components(separatedBy: ",")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.