簡體   English   中英

對於循環結果,有時會在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")
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM