尝试在 swift 中捕获变量
try catch variable in swift
我正在尝试添加针对变量的 try catch。没有尝试捕获我这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value for variable Double(label.text!)!
所以我想捕获上面的错误。我在下面试过
do{
let value = try Double(label.text!)!
print("value\(value)")
} catch{
print("hi")
}
但它仍然给出同样的错误,我也看到了这个警告:
No calls to throwing functions occur within try and catch block in unreachable...
这是在 swift 中使用 try catch 块的正确方法吗?
编辑:(不重复)如果我只是 return return Double(labelDisplay.text)
我得到编译错误 value of option type String? not unwrapped, so I have to use
return Double(labelDisplay.text!)!`是如果失败的地方。这就是我试图抓住它的原因。
另一个编辑:标签是 @IBOutlet weak private var label: UILabel!
编辑:return 代码
var displayValue: Double{
get{
print(labelDisplay.text.dynamicType)
return Double(labelDisplay.text!)!
}
set{
labelDisplay.text! = String(newValue)
}
}
我个人会使用 if let,我相信这正是您真正想要做的。
if let value = Double(label.text!)!{
print("value\(value)")
}else{
print("hi")
}
请告诉我这是否符合您的要求,如果不符合,我很乐意以任何其他方式提供帮助!
更新:
if let value = Double(label.text!){
print("value\(value)")
}else{
print("hi")
}
这是正确的方法。注意:标签文本只是展开的,而不是整个 double。如果 label.text!是一个有效的数字(“3.14159”)而不是像("hello")这样的文本,那么该值将被打印出来。如果没有,else 语句将捕获它。
更新 2:
正在工作:
声明:
var displayValue: Double{
get{
return Double(label.text!)!
}
set{
displayLabel.text! = String(newValue)
}
}
函数:
if let value = Double(label.text!){
print("value\(value)")
displayLabel.text! = "\(displayValue)"
}else{
print("hi")
}
从字符串中生成 double 不会抛出异常,所以你不会捕获任何东西,你应该
if let value = Double(label.text) {
//here it worked out
print("value \(value)")
} else {
//it failed
}
其实Swift中确实存在try-catch操作。在 Apple 网站的官方文档中,解释说应该这样做:
do {
try expression
statements
} catch pattern 1 {
statements
} catch pattern 2 where condition {
statements
}
例如:
var vendingMachine = VendingMachine()
vendingMachine.coinsDeposited = 8
do {
try buyFavoriteSnack("Alice", vendingMachine: vendingMachine)
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
}
我正在尝试添加针对变量的 try catch。没有尝试捕获我这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value for variable Double(label.text!)!
所以我想捕获上面的错误。我在下面试过
do{
let value = try Double(label.text!)!
print("value\(value)")
} catch{
print("hi")
}
但它仍然给出同样的错误,我也看到了这个警告:
No calls to throwing functions occur within try and catch block in unreachable...
这是在 swift 中使用 try catch 块的正确方法吗?
编辑:(不重复)如果我只是 return return Double(labelDisplay.text)
我得到编译错误 value of option type String? not unwrapped, so I have to use
return Double(labelDisplay.text!)!`是如果失败的地方。这就是我试图抓住它的原因。
另一个编辑:标签是 @IBOutlet weak private var label: UILabel!
编辑:return 代码
var displayValue: Double{
get{
print(labelDisplay.text.dynamicType)
return Double(labelDisplay.text!)!
}
set{
labelDisplay.text! = String(newValue)
}
}
我个人会使用 if let,我相信这正是您真正想要做的。
if let value = Double(label.text!)!{
print("value\(value)")
}else{
print("hi")
}
请告诉我这是否符合您的要求,如果不符合,我很乐意以任何其他方式提供帮助!
更新:
if let value = Double(label.text!){
print("value\(value)")
}else{
print("hi")
}
这是正确的方法。注意:标签文本只是展开的,而不是整个 double。如果 label.text!是一个有效的数字(“3.14159”)而不是像("hello")这样的文本,那么该值将被打印出来。如果没有,else 语句将捕获它。
更新 2:
正在工作:
声明:
var displayValue: Double{
get{
return Double(label.text!)!
}
set{
displayLabel.text! = String(newValue)
}
}
函数:
if let value = Double(label.text!){
print("value\(value)")
displayLabel.text! = "\(displayValue)"
}else{
print("hi")
}
从字符串中生成 double 不会抛出异常,所以你不会捕获任何东西,你应该
if let value = Double(label.text) {
//here it worked out
print("value \(value)")
} else {
//it failed
}
其实Swift中确实存在try-catch操作。在 Apple 网站的官方文档中,解释说应该这样做:
do {
try expression
statements
} catch pattern 1 {
statements
} catch pattern 2 where condition {
statements
}
例如:
var vendingMachine = VendingMachine()
vendingMachine.coinsDeposited = 8
do {
try buyFavoriteSnack("Alice", vendingMachine: vendingMachine)
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
}