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

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

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

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

弒天下 2019-10-11 10:51:41
我真的很想在我的Swift代碼中使用一個更簡單的經(jīng)典try catch塊,但是我找不到能做到這一點的任何東西。我只需要: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ù)時,某些信息仍位于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...")    }}我也嘗試過這個: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()        }    }}這是一個優(yōu)先級很低的代碼塊,我花了很多時間進行反復(fù)試驗,弄清楚swift內(nèi)置的哪種錯誤處理程序適用于在我有成千上萬種類似情況的情況下極其獨特的情況。代碼可能會崩潰,并且不會對用戶體驗產(chǎn)生任何影響。簡而言之,我不需要任何花哨的東西,但是Swift似乎有非常具體的錯誤處理程序,這些錯誤處理程序根據(jù)我是從函數(shù)返回值中獲取值還是從數(shù)組索引中獲取不存在的值而有所不同。是否像其他流行編程語言一樣,可以在Swift上進行簡單嘗試?
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經(jīng)驗 獲得超6個贊

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


運行時異常(您可能還會看到稱為trap,致命錯誤,斷言失敗等)是程序員錯誤的標(biāo)志。除了內(nèi)部-Ounchecked版本,Swift通常保證這些會使您的程序崩潰,而不是繼續(xù)在錯誤/未定義狀態(tài)下執(zhí)行。這類崩潰可能是由于強制展開!,隱式展開,unowned引用濫用,溢出,fatalError()s和precondition()s及assert()s等導(dǎo)致的整數(shù)運算/轉(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貢獻1875條經(jīng)驗 獲得超3個贊

斯威夫特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)注
  • 822 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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