对于 in 循环结果有时在 EXC_BAD_ACCESS
For in loop results sometimes in EXC_BAD_ACCESS
有时我会在这段代码中遇到 EXC_BAD_ACCESS
错误:
internal func downloadMultiple(files: NSMutableArray, remoteBaseUrl: NSURL, completion: (result: Int)->()) -> Void {
self.filesToDownload = files
self.cb = completion
for item in files { // this line gets marked, but why this line?
print("file ", item["file"] as! String)
self.download(remoteBaseUrl.URLByAppendingPathComponent(item["file"] as! String)!)
}
}
但它只是偶尔出现,知道如何找出造成这种情况的原因吗?
你遇到崩溃是因为 item[file]
是 nil
并且你正在使用强制展开,像这样使用可选绑定
for item in files {
// this line gets marked, but why this line?
if let file = item["file"] as String {
print("file ", file)
self.download(remoteBaseUrl.URLByAppendingPathComponent(file)
} else {
print("file not available")
}
}
有时我会在这段代码中遇到 EXC_BAD_ACCESS
错误:
internal func downloadMultiple(files: NSMutableArray, remoteBaseUrl: NSURL, completion: (result: Int)->()) -> Void {
self.filesToDownload = files
self.cb = completion
for item in files { // this line gets marked, but why this line?
print("file ", item["file"] as! String)
self.download(remoteBaseUrl.URLByAppendingPathComponent(item["file"] as! String)!)
}
}
但它只是偶尔出现,知道如何找出造成这种情况的原因吗?
你遇到崩溃是因为 item[file]
是 nil
并且你正在使用强制展开,像这样使用可选绑定
for item in files {
// this line gets marked, but why this line?
if let file = item["file"] as String {
print("file ", file)
self.download(remoteBaseUrl.URLByAppendingPathComponent(file)
} else {
print("file not available")
}
}