I just can't wrap my head around this error, I am trying to add a string to an array like I always do in objective-c, but swift gives me a weird error.
var fileArray:NSMutableArray = []
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
self.fileArray.addObject(self.urlTextField.text)
self.processURL()
}))
ERROR:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
How fileArray is immutable? I declare it as MutableArray !!!!!!
EDIT::: so turns out problem is the way I populate the array
fileArray = myDict?.valueForKey("fileList") as! NSMutableArray
this solved the problem
fileArray = myDict?.valueForKey("fileList")!.mutableCopy() as! NSMutableArray
An array does not become mutable just because you declare it as such or because you cast it as mutable with as! NSMutableArray. An array is only mutable if it is created as a mutable array with [[NSMutableArray alloc] ....] or by making a mutableCopy of an array.
(the same goes for dictionaries, strings, NSSet and NSData)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With