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
fileArray
object? Can you add exception breakpoint and check on what line app is crashing? – Vlad Papkoself.fileArray
by doing CMD+CLICK on it? That way you will know if it is indeed a weird Swift bug or ifself.fileArray
is maybe something else than this NSMutableArray. – Eric AyaviewLoad
from a.plist
likefileArray = myDict?.valueForKey("fileList") as! NSMutableArray
Do you think that might be the problem – Mord Fustangas! NSMutableArray
only makes compiler silent but not makes actual value to be mutable. You need callmutableCopy
on result ofmyDict?.valueForKey("fileList")
call. – Vlad PapkofileArray = myDict?.valueForKey("fileList")!.mutableCopy() as! NSMutableArray
– Subbu