mnewmedia mnewmedia - 4 days ago 5
Swift Question

why to i have to cast NSMutableArray to NSMutableArray?

In my function arguments I declared that

files
argument has to be of type
NSMutableArray
. Why does Xcode then say that the
files
argument is of typ
AnyObject
and I have to cast it to
NSMutableArray
?

internal func downloadMultiple(files: NSMutableArray, remoteBaseUrl: NSURL, completion: (result: Int)->()) -> Void {
self.filesToDownload = files.mutableCopy() as! NSMutableArray // why this cast necessary?
self.cb = completion

for item in files {
print("file ", item["file"] as! String)
self.download(remoteBaseUrl.URLByAppendingPathComponent(item["file"] as! String)!)
}
}

Answer

mutableCopy is implemented by NSObject and defined as:

func mutableCopy() -> Any

So therefore when you call mutableCopy() the resulting object needs to be cast.

Comments