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

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

通過可選綁定在Swift中進(jìn)行安全(邊界檢查)數(shù)組查找?

通過可選綁定在Swift中進(jìn)行安全(邊界檢查)數(shù)組查找?

慕婉清6462132 2019-08-09 15:45:41
通過可選綁定在Swift中進(jìn)行安全(邊界檢查)數(shù)組查找?如果我在Swift中有一個(gè)數(shù)組,并嘗試訪問超出范圍的索引,則會(huì)出現(xiàn)一個(gè)不足為奇的運(yùn)行時(shí)錯(cuò)誤:var str = ["Apple", "Banana", "Coconut"]str[0] // "Apple"str[3] // EXC_BAD_INSTRUCTION但是,我會(huì)想到Swift帶來的所有可選鏈接和安全性,這樣做會(huì)很簡(jiǎn)單:let theIndex = 3if let nonexistent = str[theIndex] { // Bounds check + Lookup    print(nonexistent)     ...do other things with nonexistent...}代替:let theIndex = 3if (theIndex < str.count) {         // Bounds check    let nonexistent = str[theIndex] // Lookup    print(nonexistent)        ...do other things with nonexistent... }但事實(shí)并非如此 - 我必須使用ol' if語(yǔ)句來檢查并確保索引小于str.count。我嘗試添加自己的subscript()實(shí)現(xiàn),但我不知道如何將調(diào)用傳遞給原始實(shí)現(xiàn),或者不使用下標(biāo)符號(hào)來訪問項(xiàng)目(基于索引):extension Array {     subscript(var index: Int) -> AnyObject? {         if index >= self.count {             NSLog("Womp!")             return nil         }         return ... // What?    }}
查看完整描述

3 回答

?
莫回?zé)o

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

我偶然發(fā)現(xiàn)了一種更好的實(shí)現(xiàn)此功能的方法:

Swift 3.2和更新版本

extension Collection {

    /// Returns the element at the specified index if it is within bounds, otherwise nil.    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }}

Swift 3.0和3.1

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

    /// Returns the element at the specified index if it is within bounds, otherwise nil.    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }}

斯威夫特2

extension CollectionType {

    /// Returns the element at the specified index if it is within bounds, otherwise nil.    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }}

let array = [1, 2, 3]for index in -20...20 {
    if let item = array[safe: index] {
        print(item)
    }}


查看完整回答
反對(duì) 回復(fù) 2019-08-09
?
慕少森

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

如果你真的想要這種行為,它就像你想要一個(gè)Dictionary而不是一個(gè)數(shù)組。字典nil在訪問丟失的密鑰時(shí)返回,這是有道理的,因?yàn)橐烂荑€是否存在于字典中要困難得多,因?yàn)檫@些密鑰可以是任何東西,在數(shù)組中密鑰必須在以下范圍內(nèi):0to count。迭代這個(gè)范圍是非常常見的,你可以絕對(duì)肯定在循環(huán)的每次迭代中都有一個(gè)真正的值。

我認(rèn)為它不能以這種方式工作的原因是Swift開發(fā)人員做出的設(shè)計(jì)選擇。舉個(gè)例子:

var fruits: [String] = ["Apple", "Banana", "Coconut"]var str: String = "I ate a \( fruits[0] )"

如果您已經(jīng)知道索引存在,就像在大多數(shù)使用數(shù)組的情況下一樣,這段代碼很棒。但是,如果訪問標(biāo)可能可能返回nil,那么你已經(jīng)改變了返回類型Arraysubscript方法是可選的。這會(huì)將您的代碼更改為:

var fruits: [String] = ["Apple", "Banana", "Coconut"]var str: String = "I ate a \( fruits[0]! )"//                                     ^ Added

這意味著每次迭代數(shù)組時(shí)都需要解包一個(gè)可選項(xiàng),或者使用已知索引執(zhí)行任何其他操作,因?yàn)楹苌儆腥丝梢栽L問超出范圍的索引。Swift設(shè)計(jì)者在訪問越界索引時(shí)以犧牲運(yùn)行時(shí)異常為代價(jià),選擇了較少的可選解包。崩潰比nil你在某個(gè)地方?jīng)]想到的邏輯錯(cuò)誤更可取。

我同意他們的觀點(diǎn)。因此,您將不會(huì)更改默認(rèn)Array實(shí)現(xiàn),因?yàn)槟鷮⑵茐乃行枰獊碜詳?shù)組的非可選值的代碼。

相反,您可以子類化Array,并覆蓋subscript以返回可選項(xiàng)?;蛘撸鼘?shí)際地,您可以Array使用執(zhí)行此操作的非下標(biāo)方法進(jìn)行擴(kuò)展。

extension Array {

    // Safely lookup an index that might be out of bounds,    // returning nil if it does not exist    func get(index: Int) -> T? {
        if 0 <= index && index < count {
            return self[index]
        } else {
            return nil
        }
    }}var fruits: [String] = ["Apple", "Banana", "Coconut"]if let fruit = fruits.get(1) {
    print("I ate a \( fruit )")
    // I ate a Banana}if let fruit = fruits.get(3) {
    print("I ate a \( fruit )")
    // never runs, get returned nil}

Swift 3更新

func get(index: Int) ->T? 需要被替換 func get(index: Int) ->Element?


查看完整回答
反對(duì) 回復(fù) 2019-08-09
  • 3 回答
  • 0 關(guān)注
  • 675 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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