第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何在Swift中捕獲“索引超出范圍”?

如何在Swift中捕獲“索引超出范圍”?

弒天下 2019-10-11 10:51:41
我真的很想在我的Swift代碼中使用一個(gè)更簡單的經(jīng)典try catch塊,但是我找不到能做到這一點(diǎn)的任何東西。我只需要:try {// some code that causes a crash.}catch {// okay well that crashed, so lets ignore this block and move on.}  這是我的難題,當(dāng)TableView重新加載新數(shù)據(jù)時(shí),某些信息仍位于RAM中,該信息會調(diào)用didEndDisplayingCell具有新的空數(shù)據(jù)源的tableView崩潰。所以我經(jīng)常拋出異常 Index out of range我已經(jīng)試過了:func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {    do {        let imageMessageBody = msgSections[indexPath.section].msg[indexPath.row] as? ImageMessageBody        let cell = tableView.dequeueReusableCellWithIdentifier("ImageUploadCell", forIndexPath: indexPath) as! ImageCell        cell.willEndDisplayingCell()    } catch {        print("Swift try catch is confusing...")    }}我也嘗試過這個(gè):func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {    print(indexPath.section)    print(indexPath.row)    if msgSections.count != 0 {        if let msg = msgSections[indexPath.section].msg[indexPath.row] as? ImageMessageBody {            let cell = tableView.dequeueReusableCellWithIdentifier("ImageUploadCell", forIndexPath: indexPath) as! ImageCell            cell.willEndDisplayingCell()        }    }}這是一個(gè)優(yōu)先級很低的代碼塊,我花了很多時(shí)間進(jìn)行反復(fù)試驗(yàn),弄清楚swift內(nèi)置的哪種錯(cuò)誤處理程序適用于在我有成千上萬種類似情況的情況下極其獨(dú)特的情況。代碼可能會崩潰,并且不會對用戶體驗(yàn)產(chǎn)生任何影響。簡而言之,我不需要任何花哨的東西,但是Swift似乎有非常具體的錯(cuò)誤處理程序,這些錯(cuò)誤處理程序根據(jù)我是從函數(shù)返回值中獲取值還是從數(shù)組索引中獲取不存在的值而有所不同。是否像其他流行編程語言一樣,可以在Swift上進(jìn)行簡單嘗試?
查看完整描述

3 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

斯威夫特的錯(cuò)誤處理(do/ try/ catch)是不是要解決運(yùn)行時(shí)異常,如“索引超出范圍”。


運(yùn)行時(shí)異常(您可能還會看到稱為trap,致命錯(cuò)誤,斷言失敗等)是程序員錯(cuò)誤的標(biāo)志。除了內(nèi)部-Ounchecked版本,Swift通常保證這些會使您的程序崩潰,而不是繼續(xù)在錯(cuò)誤/未定義狀態(tài)下執(zhí)行。這類崩潰可能是由于強(qiáng)制展開!,隱式展開,unowned引用濫用,溢出,fatalError()s和precondition()s及assert()s等導(dǎo)致的整數(shù)運(yùn)算/轉(zhuǎn)換(以及不幸的是,Objective-C異常)引起的。


解決方法是簡單地避免這些情況。在您的情況下,檢查數(shù)組的邊界:


if indexPath.section < msgSections.count && indexPath.row < msgSections[indexPath.section].msg.count {

    let msg = msgSections[indexPath.section].msg[indexPath.row]

    // ...

}

(或者,正如rmaddy在評論中說的那樣-調(diào)查為什么會發(fā)生此問題!它根本不應(yīng)該發(fā)生。)


查看完整回答
反對 回復(fù) 2019-10-11
?
翻過高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊

斯威夫特4:

extension Collection where Indices.Iterator.Element == Index {

    subscript (exist index: Index) -> Iterator.Element? {

        return indices.contains(index) ? self[index] : nil

    }

}

用法:


var index :Int = 6 // or whatever number you need

if let _ = myArray[exist: index] {

   // do stuff

}

要么


var index :Int = 6 // or whatever number you need

guard let _ = myArray[exist: index] else { return }


查看完整回答
反對 回復(fù) 2019-10-11
  • 3 回答
  • 0 關(guān)注
  • 837 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號