小编bol*_*nad的帖子

如何在swift 2.0中处理连续多次尝试

我有一块代码需要执行2个需要尝试的语句.嵌套try是否更好,每个人都有自己的do {} catch {}

 do { 
     try thingOne()
     do {
         try thingTwo()
     } catch let error as NSError {
          //handle this specific error
     }
 } catch let error as NSError {
      //handle the other specific error here
 } 
Run Code Online (Sandbox Code Playgroud)

...或者将try的包装在一个块中并连续运行它们?

do {

   try thingOne()
   try thingTwo()
} catch let error as NSError {
    //do something with this error
}
Run Code Online (Sandbox Code Playgroud)

第二种情况似乎比第一种情况更容易阅读,但catch如果其中任何一种引发错误,那么它会起作用吗?

然后我需要区分抛出的不同错误,除非错误足够通用,那么它可能无关紧要.查看Apple文档并没有看到任何与此相关的内容.

nested try-catch swift2

21
推荐指数
1
解决办法
3586
查看次数

Swift可选方法参数,但至少需要填充一个参数

我正在使用2个参数在swift中创建一个类的方法,它们都是可选的.但是,我需要至少填充其中一个才能使方法成功运行,哪个无关紧要

func someMethod(arg1: Sometype?, arg2: Sometype?)-> Void {
        //I need at least one argument to 
        //be populated to do what i need
}
Run Code Online (Sandbox Code Playgroud)

在Objective-c中,如果这两个对象都是零,我们可以抛出一个Assert.在Swift中,我想知道是否有更好的方法来做这个而不是断言.

methods optional-parameters swift

6
推荐指数
1
解决办法
571
查看次数

标签 统计

methods ×1

nested ×1

optional-parameters ×1

swift ×1

swift2 ×1

try-catch ×1