Swift 中的关键字 "throws" 是什么意思?
What is the meaning of keyword "throws" in Swift?
这里的"throws"关键字是什么意思:
此代码执行时间很长,我认为上图中的 "throws" 关键字相关:
let url = URL(string:"\(APIs.postsImages)\(postImg)")
let imgData = try? Data.init(contentsOf: url)
self.postImage.image = UIImage.init(data: imgData)
throws
关键字表示函数可能会抛出错误。也就是说,它是一个 投掷函数 。有关详细信息,请参阅 the documentation。
init() throws
可以 return 关于失败的更多信息,让调用者决定他们是否关心它。阅读有关它的有用 post here。
To indicate that a function, method, or initializer can throw an error, you need to write the throws
keyword in the function’s declaration after its parameters. A function marked with throws
is called a throwing function. If the function specifies a return type, you write the throws keyword before the return arrow.
func thisFunctionCanThrowErrors() throws -> String
或者在实际代码中可能如下所示:
enum PossibleErrors: Error {
case wrongUsername
case wrongPassword
}
func loggingIn(name: String, pass: String) throws -> String {
if name.isEmpty {
throw PossibleErrors.wrongUsername
}
if pass.isEmpty {
throw PossibleErrors.wrongPassword
}
return "Fine"
}
像这样的 throwing functions
必须使用以下尝试运算符之一调用:
- 试试
- 试试?
- 试试!
使用 init() throws
你可以中断 class 初始化程序的执行,而不需要填充所有存储的属性:
class TestClass {
let textFile: String
init() throws {
do {
textFile = try String(contentsOfFile: "/Users/swift/text.txt",
encoding: NSUTF8StringEncoding)
catch let error as NSError {
throw error
}
}
}
并且在结构中你甚至可以避免 do/catch
块:
struct TestStruct {
var textFile: String
init() throws {
textFile = try String(contentsOfFile: "/Users/swift/text.txt",
encoding: NSUTF8StringEncoding)
}
}
这里的"throws"关键字是什么意思:
此代码执行时间很长,我认为上图中的 "throws" 关键字相关:
let url = URL(string:"\(APIs.postsImages)\(postImg)")
let imgData = try? Data.init(contentsOf: url)
self.postImage.image = UIImage.init(data: imgData)
throws
关键字表示函数可能会抛出错误。也就是说,它是一个 投掷函数 。有关详细信息,请参阅 the documentation。
init() throws
可以 return 关于失败的更多信息,让调用者决定他们是否关心它。阅读有关它的有用 post here。
To indicate that a function, method, or initializer can throw an error, you need to write the
throws
keyword in the function’s declaration after its parameters. A function marked withthrows
is called a throwing function. If the function specifies a return type, you write the throws keyword before the return arrow.
func thisFunctionCanThrowErrors() throws -> String
或者在实际代码中可能如下所示:
enum PossibleErrors: Error {
case wrongUsername
case wrongPassword
}
func loggingIn(name: String, pass: String) throws -> String {
if name.isEmpty {
throw PossibleErrors.wrongUsername
}
if pass.isEmpty {
throw PossibleErrors.wrongPassword
}
return "Fine"
}
像这样的 throwing functions
必须使用以下尝试运算符之一调用:
- 试试
- 试试?
- 试试!
使用 init() throws
你可以中断 class 初始化程序的执行,而不需要填充所有存储的属性:
class TestClass {
let textFile: String
init() throws {
do {
textFile = try String(contentsOfFile: "/Users/swift/text.txt",
encoding: NSUTF8StringEncoding)
catch let error as NSError {
throw error
}
}
}
并且在结构中你甚至可以避免 do/catch
块:
struct TestStruct {
var textFile: String
init() throws {
textFile = try String(contentsOfFile: "/Users/swift/text.txt",
encoding: NSUTF8StringEncoding)
}
}