Swift 2错误处理及时 [英] Swift 2 Error handling and while

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

问题描述

例如以下代码:

while let data = Provider.getData() {
    ...
}

使用Swift 2,您会遇到两个错误:
条件绑定的初始化程序必须具有可选类型,而不是'String'
可以抛出呼叫,但未将其标记为"try",并且未处理错误.

With Swift 2 you get two errors:
Initializer for conditional binding must have Optional type, not 'String'
Call can throw, but it is not marked with 'try' and the error is not handled.

在这里进行错误处理的最佳解决方案是什么?
…如果我要处理此方法中的错误.
…如果我想抛出错误.

What is the best solution to do error handling here…
…if I want to handle the error in this method.
…if I want to throw the error up.

推荐答案

您的getData()函数返回String值,而不是可选值.因此,您应该使用?将getData函数的返回类型更改为可选值类型.运算符

虽然let总是期望一个可选值,但是如果您的getData函数总是返回一个字符串值,那么使用while let没有意义,因为您是故意告诉编译器getData函数将总是返回一个String值并试图对其进行拆包,因此我们不应解开非可选值.

while let always expects an optional value, if your getData function always returns a string value then using while let makes no sense because you are telling the compiler intentionally that getData function will always return a String value and trying to unwrap it, so we shouldn't unwrap a nonoptional value.

错误处理代码(要紧记 Swifter ) :

private func nextLine() throws -> String?{
     var returnData : String? = ""

     if arc4random_uniform(7) != 3 {
     returnData = "Genreated Random number other than 3"
     }else{
         throw NSError(domain: "Generated random number 3", code: 111, userInfo: nil)
      }
     return returnData
}

do {
     while let headerLine =  try nextLine() {
     //do something with the header
     print(headerLine)
     }
 }catch{

       //Handle exception
       print(error)
 }

nextLine函数返回一个字符串,告诉它生成的随机数不是3",如果生成的数字不等于3,否则它将引发可以在catch块中处理的异常.返回一个可选值.如果我删除了?从nextLine函数的返回类型开始.这会告诉您条件值的初始化程序必须具有可选类型而不是String"的错误消息,这意味着编译器试图解开没有意义的非可选值.

nextLine function returns a string telling "Generated Random number other than 3" if generated number is not equal to 3,or else it will throw an exception which can be handled in the catch block.Here I have potentially made nextLine function to return an optional value.If I remove ? from return type of nextLine function. It will give you an error telling "Initializer for conditional value must have optional type not String", it means compiler is trying to unwrap a non optional value which makes no sense.

考虑:

  var someIntegerValue = 5

  if let x = someIntegerValue
  {
    // it will give an error
  }

上面的代码会给您一个错误,告诉您用于条件绑定的Intializer必须具有可选类型,而不是Int",因为即使在这里,我们也试图解开一个非可选值.

Above code will give you an error telling "Intializer for conditional binding must have Optional type,not Int",because even here we are trying to unwrap a non optional value.

 If you replace var some = 5 with var some : Int? = 5 it will be all right.

错误/异常处理:

您可以在提取应该在 do 块中反写的值之前使用 try 关键字,它将获取值或将引发异常,异常应该在 catch 块内处理.

you can make use of try keyword before fetching the value which should be inturn written inside do block ,it will either fetch value or it will fire an exception,exception should be handled inside catch block.

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

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