Swift2.0语法更新(1)

1. Error Handling

在Swift中,用满足ErrorType协议类型来表示Error。

enum  VendingMachingError:  ErrorType  {
    case  InvalidSelection
    case  InsufficientFunds(required: Double)
    case  OutOfStock
}

为了表明一个函数或者方法会抛出错误,你需要在它的声明之后添加throws关键字,如果不提前声明,则不可抛出错误。

func  canThrowErrors()  throws  -> String
func  cannotThrowErrors() -> String

在可抛出错误的函数体中的任意位置都可用throw语句抛出错误。

struct  Item {
    
var  price: Double
    
var  count: Int
}

var  inventory = [
    
"Candy Bar" : Item (price: 1.25 , count: 7 ),
    
"Chips" : Item (price: 1.00 , count: 4 ),
    
"Pretzels" : Item (price: 0.75 , count: 11 )
]

var  amountDeposited = 1.00

func  vend(itemNamed name: String ) throws  {
    
guard var  item = inventory [name] else  {
        
throw
            
VendingMachingError . InvalidSelection
    }
    
    
guard  item. count  >  0 else  {
        
throw VendingMachingError . OutOfStock
    }
    
    
if amountDeposited  >= item. price  {
        
amountDeposited  -= item. price
        --item.
count
        
inventory [name] = item
    } 
else  {
        
let  amountRequired = item. price  -  amountDeposited
        
throw
            
VendingMachingError . InsufficientFunds (required: amountRequired)
    }
}

在你调用一个可能throw的函数时,你需要在前面加上try关键字。

let  favoriteSnacks = [
    
"Alice" : "Chips" ,
    
"Bob" : "Licorice" ,
    
"Eve" : "Pretzels"
]

func  buyFavoriteSnacks(person: String ) throws  {
    
let  snackName = favoriteSnacks [person] ?? "Candy Bar"
    
try vend (itemNamed: snackName)
}

如果在某些情况下你知道一些可抛出错误的函数在运行时不会抛出错误,你可以使用try!关键字来代替普通的try。

func  willOnlyThrowIfTrue(value:  Bool throws  {
    
if  value {
        
throw  someError
    }
    
do  {
        
try   willOnlyThrowIfTrue ( false )
    } 
catch  {
        
//Handle Error
    }
}

try willOnlyThrowIfTrue ( false )

defer关键字内的语句直到离开当前代码块时才会被执行。这些语句让你能做一些必要的清理工作,不论当前代码块内有没有错误出现,defer内的语句都会执行。而且defer的执行顺序和它们被定义的顺序是相反的,比如第一个定义的defer会在第二个定义的defer之后执行。

func  processFile(filename:  String throws  {
    
if  exists(filename) {
        
let  file = open(filename)
    }
    
defer  {
        
close (file)
    }
    
while   let  line =  try  file.readline() {
        
//Work with the file.
    }
    
//close(file) is called here, at the end of the scope.
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值