如果使用NSSetUncaughtExceptionHandler,它只处理objective-C运行时错误。https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/index.html#//apple_ref/c/func/NSSetUncaughtExceptionHandler
NSSetUncaughtExceptionHandler可以捕获异常:
var a: NSArray = [""]
println(a[2])但是NSSetUncaughtExceptionHandler不能捕获异常:
var a = [""]
println(a[2])如何快速处理非objective-C运行时错误( swift运行时错误)?
发布于 2019-08-05 19:06:26
如果您希望处理超出索引的异常,则始终可以验证索引项
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
public subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}发布于 2015-06-24 21:43:16
已经有一个类似的问题Error-Handling in Swift-Language得到了详尽的回答。请看第一个答案,其中包括Swift 2.0中有关此主题的最新更新。
https://stackoverflow.com/questions/31024803
复制相似问题