An NSArray is immutable. Once created, it can't be modified.
That's the purpose of NSMutableArray, to be modifiable.
var myArr = NSMutableArray()
myArr.addObject("hey")
You also could use a Swift typed array:
var swiftArray = [String]()
swiftArray.append("hey")
EDIT:
There's something screwy going on with the compiler. I think there's a bug in Xcode 6.3/Swift 1.2.
This code should work:
let aString:String? = "One,Two,Three"
let array = aString?.componentsSeparatedByString(",") as NSArray
But it complains
[String]? is not convertible to 'NSArray'
Suggesting that in Swift, componentsSeparatedByString returns a string array optional ([String]?]).
If that was the case, you should be able to fix it like this:
let array = aString?.componentsSeparatedByString(",")! as NSArray
But that gives this error:
error: operand of postfix '!' should have optional type; type is '[String]'
Very strange.
If you look in the headers, the Swift definition of componentsSeparatedByString() is
func componentsSeparatedByString(separator: String) -> [String]
It's pretty clear that componentsSeparatedByString() returns a Swift String array, not an optional. The compiler complains with either syntax.
This code does work:
let string1:String? = "One,Two,Three"
let array1 = string1?.componentsSeparatedByString(",")
let aMutableArray = (array1! as NSArray).mutableCopy()
as! NSMutableArray
That is your solution.