ios 如何在 Swift 中捕获“索引超出范围”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37222811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:04:19  来源:igfitidea点击:

How do I catch "Index out of range" in Swift?

iosswiftuitableview

提问by Matt Andrzejczuk

I would really like to use a more simple classic try catch block in my Swift code but I can't find anything that does it.

我真的很想在我的 Swift 代码中使用一个更简单的经典 try catch 块,但我找不到任何可以做到的。

I just need:

我只需要:

try {
// some code that causes a crash.
}
catch {
// okay well that crashed, so lets ignore this block and move on.
}  

Here's my dilema, when the TableView is reloaded with new data, some information is still siting in RAM which calls didEndDisplayingCellon a tableView with a freshly empty datasource to crash.

这是我的困境,当 TableView 用新数据重新加载时,一些信息仍然位于 RAM 中,它调用didEndDisplayingCell具有新空数据源的 tableView 崩溃。

So I frequently thrown the exception Index out of range

所以我经常抛出异常 Index out of range

I've tried this:

我试过这个:

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...")
    }
}

I've also tried this:

我也试过这个:

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()
        }
    }
}

This is a very low priority block of code, and I have wasted a lot of time with trial and error figuring out which error handler built into swift works for what seems to be extremely unique situations when I have tons of scenarios just like this one where the code can crash and it will not have any effect on the user experience.

这是一个非常低优先级的代码块,我浪费了很多时间在反复试验中找出 swift 中内置的哪个错误处理程序适用于似乎非常独特的情况,当我有很多这样的场景时代码可能会崩溃,并且不会对用户体验产生任何影响。

In short, I don't need anything fancy but Swift seems to have extremely specific error handlers that differ based on whether I'm getting a value from a functions return value or getting a value from an array's index which may not exist.

简而言之,我不需要任何花哨的东西,但 Swift 似乎有非常具体的错误处理程序,根据我是从函数返回值获取值还是从可能不存在的数组索引获取值而有所不同。

Is there a simple try catch on Swift like every other popular programming language?

是否有像其他流行的编程语言一样对 Swift 进行简单的尝试?

回答by Penkey Suresh

As suggested in comments and other answers it is better to avoid this kind of situations. However, in some cases you might want to check if an item exists in an array and if it does safely return it. For this you can use the below Array extension for safely returning an array item.

正如评论和其他答案中所建议的那样,最好避免这种情况。但是,在某些情况下,您可能想要检查数组中是否存在某个项目以及它是否安全地返回它。为此,您可以使用以下 Array 扩展来安全地返回数组项。

Swift 3

斯威夫特 3

extension Collection where Indices.Iterator.Element == Index {
    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

Swift 2

斯威夫特 2

extension Array {
    subscript (safe index: Int) -> Element? {
        return indices ~= index ? self[index] : nil
    }
}
  • This way you'll never hitIndex out of range
  • You'll have to check if the item is nil
  • 这样你永远不会打Index out of range
  • 您必须检查该项目是否 nil

refer this questionfor more

参考这个问题了解更多



Trying the Swift3 code in a Playground in Xcode 8.3.2 still leads to a "crash" when I do let ar = [1,3,4], then let v = ar[5]. Why? – Thomas Tempelmann May 17 at 17:40

当我让 ar = [1,3,4],然后让 v = ar[5] 时,在 Xcode 8.3.2 的 Playground 中尝试 Swift3 代码仍然会导致“崩溃”。为什么?– Thomas Tempelmann 5 月 17 日 17:40

You have to use our customized subscript so instead of let v = ar[5], it wll be let v = ar[safe: 5].

你必须使用我们定制的标所以不是let v = ar[5],它是无线本地环路let v = ar[safe: 5]

Default getting value from array.

默认从数组中获取值。

let boo = foo[index]

Add use the customized subscript.

添加使用自定义下标。

let boo = fee[safe: index]

// And we can warp the result using guard to keep the code going without throwing the exception.
guard let boo = foo[safe: index] else {
  return
}

回答by jtbandes

Swift's Error Handling(do/try/catch) is notthe solution to runtime exceptionslike "index out of range".

斯威夫特的错误处理do/ try/ catch)是解决运行时异常,如“索引超出范围”。

A runtime exception (you might also see these called trap, fatal error, assertion failure, etc.) is a sign of programmer error. Except in -Ouncheckedbuilds, Swift usually guarantees that these will crashyour program, rather than continuing to execute in a bad/undefined state. These sorts of crashes can arise from force-unwrapping with !, implicit unwrapping, misuse of unownedreferences, integer operations/conversions which overflow, fatalError()s and precondition()s and assert()s, etc. (And, unfortunately, Objective-C exceptions.)

运行时异常(您可能还会看到这些称为陷阱致命错误断言失败等)是程序员错误的标志。除了在-Ounchecked构建中,Swift 通常保证这些会导致程序崩溃,而不是在错误/未定义状态下继续执行。这些类型的崩溃可能源于强制解包 with !、隐式解包、滥用unowned引用、溢出的整数运算/转换、fatalError()s 和precondition()s 和assert()s 等(不幸的是,Objective-C 异常。)

The solution is to simply avoid these situations. In your case, check the bounds of the array:

解决方案是简单地避免这些情况。在您的情况下,请检查数组的边界:

if indexPath.section < msgSections.count && indexPath.row < msgSections[indexPath.section].msg.count {
    let msg = msgSections[indexPath.section].msg[indexPath.row]
    // ...
}

(Or, as rmaddy says in comments — investigate whythis problem is occurring! It really shouldn't happen at all.)

(或者,正如 rmaddy 在评论中所说 - 调查为什么会出现这个问题!它真的不应该发生。)

回答by Alessandro Ornano

Swift 4:

斯威夫特 4:

extension Collection where Indices.Iterator.Element == Index {
    subscript (exist index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

Usage:

用法

var index :Int = 6 // or whatever number you need
if let _ = myArray[exist: index] {
   // do stuff
}

or

或者

var index :Int = 6 // or whatever number you need
guard let _ = myArray[exist: index] else { return }

回答by Nimish Sharma

You can try a different approach to this. Will surely work!

您可以尝试不同的方法。肯定会工作!

if msgSections != nil {
    for msg in msgSections[indexPath.section] {
        if msgSections[indexPath.section].index(of: msg) == indexPath.row {
            (Code)
        }
}