Swift中的运行时错误处理 [英] Runtime error handling in Swift

查看:188
本文介绍了Swift中的运行时错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全知道Swift没有一个try / catch机制来捕获异常(好的,Swift 2.0现在支持它们)。我也明白许多API方法返回一个NSError,如果出现问题,将会填充一个错误对象。所以请不要指出这个问题: Swift语言中的错误处理



但是,这仍然不能解释如何对您自己的代码中的运行时错误做出反应,例如数组超出边界访问或强制展开可选值是零例如:

  var test:String? 
test = nil
println(test!)// oops!

  var arr = [0,1,2] 
for i = 0 ... 3 {
println(arr [i])// oops!
}

每个程序员偶尔会出现这样的错误,应该有一种方法来至少记录他们供以后分析。在调试时,Xcode可以向我们展示这些内容,但是如果这种情况发生在最终用户或beta测试人员身上?在纯C中有信号处理,也可以在Objective-C中使用。在斯威夫特有这样的事情吗?在应用程序死亡之前输入集中式回调?



更新:



让我重新表述一个问题:大项目,手动检查每个循环上的错误是不可行的,并且强制展开。当运行时错误最终发生时,是否有回调像Objective C的segfault处理或NSSetUncaughtExceptionHandler将被调用,以便错误可以记录/电子邮件与崩溃的堆栈跟踪?

解决方案

编辑:这个答案没有用swift 2.0更新。因为swift现在有错误处理我没有更新下面的答案。错误处理的一些方面将在将来使用swift 3.0进行更新。您可以按照这个答案以Swift语言处理错误



Swift被设为 typeSafe language.It在编译时得到错误,而不是等待在运行时导致错误。



在第一个例子中,您使用可选

  var test:String? 

首先明白可选的含义。当你指定可选你说它可以是没有或没有价值。当你解开测试你说我知道这个值是不是nil 。请打开它,我确信这是你的责任,看看它在哪里code> nil 。如果你不确定那个比你应该使用可选绑定这里。当你不确定价值总是使用条件,而打破

 如果let notNilTest = test {
// use notNilTest
}
else {
// handle error
}

在第二个例子中有意义的是运行时异常处理,但是如果条件具有count.So在第二个示例中,您可以轻松地获得这一点,作为开发人员,如果条件获取 count 的数组。



从swift指南:


如果您尝试使用下标语法来检索或设置不在数组现有边界之外的
索引的值,那么您将触发
a运行时错误。但是,通过将它与数组的count属性进行比较,可以检查索引在
之前是否有效。除非
计数为0(意味着数组为空),否则
数组中最大的有效索引将始终为count - 1,因为数组从零开始索引。


他们清楚地提到这一点,你应该关心这些事情,使你的代码减少错误。他们提供了一些事情,我们应该知道如何使用这些东西。


I am fully aware that Swift doesn't have a try/catch mechanism to catch exceptions (OK, Swift 2.0 now supports them). I also understand that many API methods return a NSError that will be filled with an error object if something goes wrong. So please don't point me to this question: Error-Handling in Swift-Language

But this still doesn't explain how to react to runtime errors in your own code, like array-out-of-bounds accesses or force-unwrapping an optional value that is nil. For example:

var test: String?
test = nil
println(test!) //oops!

or

var arr = [0,1,2]
for i = 0...3 {
    println(arr[i]) //oops!
}

Every programmer makes such mistakes occasionally and there should be a way to at least log them for later analysis. While debugging, Xcode can show us those, but what if this happens to an end-user or beta-tester? In pure C there is signal handling and it could be used in Objective-C as well. Is there something like this in Swift? A centralized callback entered just before the app dies?

Update:

Let me rephrase the question: in a large project, it is not feasible to manually check for the above errors on every loop and force-unwrapping. When a runtime error does happen eventually, is there a callback like Objective C's segfault handling or NSSetUncaughtExceptionHandler that will get called so that the error can be logged/e-mailed together with a stacktrace of the crash?

解决方案

Edit: This answer is not updated with swift 2.0. As swift now has error handling I have not updated the below answer. Some aspect of error handling will be updated in future with swift 3.0. You can follow this answer Error-Handling in Swift-Language

Swift is made to be typeSafe language.It get error at compile time rather than waiting to cause at runtime.

In first example you are using Optional.

var test: String?

First understand meaning of optional.When you specifying optional you are saying it could be nil or have no value.Now when you unwrapping test you are saying i know this value is not nil.Please unwrap it i am sure about that.So its your responsibility to see where it nil.If you are not sure about that than you should use optional binding here.When you are unsure about value always use if condition while unwrrapping

  if let notNilTest = test {
    //use notNilTest
  }
  else{
   //handle error
  }

In second example it should make sense to have the runtime exception handling but you can easily get this with if condition having count.So in second example as developer you should use if condition to get count of array.

From swift guide:

If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error. However, you can check that an index is valid before using it, by comparing it to the array’s count property. Except when count is 0 (meaning the array is empty), the largest valid index in an array will always be count - 1, because arrays are indexed from zero.

They clearly mention about this and you should take care of these things to make your code less buggy.Some things they have provided and we should know about how to use these things.

这篇关于Swift中的运行时错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆