您的位置:首页 > 移动开发 > Swift

Swift2.0语法更新(1)

2015-12-15 12:49 435 查看
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:Doublevar 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.00func vend(itemNamed name:String)throws {

guardvar item
=
inventory[name]else {

throwVendingMachingError.InvalidSelection

}

guard item.count > 0else {

throwVendingMachingError.OutOfStock

}

ifamountDeposited >=
item.
price {

amountDeposited -= item.price

--item.
countinventory[name] = item

}
else {

let amountRequired = item.price - amountDepositedthrowVendingMachingError.InsufficientFunds(required:
amountRequired)

}


}

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

let favoriteSnacks = [

"Alice":"Chips",

"Bob":"Licorice",

"Eve":"Pretzels"

]

func buyFavoriteSnacks(person:String)throws {

let snackName =favoriteSnacks[person]
??
"Candy Bar"tryvend(itemNamed:
snackName)

}


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

func willOnlyThrowIfTrue(value: Bool) throws {

if value {

throw someError

}

do {

trywillOnlyThrowIfTrue(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)

}

whilelet line
=
try file.readline() {

//Work with the file.

}

//close(file) is called here, at the end of the scope.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: