如何验证 IBOutlet/var 是否为 nil?
How do I validate IBOutlet/var for nil?
我使用 Swift 和 Xcode 6.1.1 编写了一个简单的应用程序。该程序是一个简单的计算器,运行良好,但我无法验证三个文本字段的非零输入。因此,如果用户将该字段留空然后点击 "calculate,",应用程序就会崩溃。
该应用接受三个输入,最初是字符串。我写了一个 if 语句来检查 nil 但它不起作用 - 无论如何它都会传递给 else。这是与我的问题相关的代码块:
...
@IBOutlet var calcButton: UIBarButtonItem!
@IBOutlet var yearOneField: UITextField!
@IBOutlet var yearTwoField: UITextField!
@IBOutlet var yearThreeField: UITextField!
@IBOutlet var displayWindow: UILabel!
@IBAction func calcButtonTapped(sender: AnyObject) {
if (yearOneField == nil) {
displayWindow.text = ("Non-zero entries are not permitted. Please enter new values.")
} else {
let yearOne = yearOneField.text.toInt()
let yearTwo = yearTwoField.text.toInt()
let yearThree = yearThreeField.text.toInt()
...
我想我可以评估 IBOutlet 为 nil,但那没有用。我是 Swift 和 Xcode 的新手,所以我希望这对那里更有经验的开发人员来说是一个 n00b 问题。谢谢。
文本字段本身永远不会是 nil
。它们是在初始化期间创建和分配的,您永远不会删除它们。
我想你想检查它们的 text
属性是否包含任何文本,你可以这样做:
已为 Swift 2 更新:
if let text = yearOneField.text where !text.isEmpty {
// perform the conversions
} else {
// the text field is empty
}
您可以使用 guard
:
避免嵌套
guard let text = yearOneField.text where !text.isEmpty else {
// the text field is empty
return
}
// perform the conversions
我更喜欢 guard
语法,因为它更清楚 ideal 结果是什么。
如果您忘记在 Interface Builder 中连接它们,@IBOutlet
可能会是 nil
的唯一方法。通常您不需要检查,因为崩溃会告诉您解决该问题。
toInt()
函数 return 是一个可选的 Int
(又名 Int?
),在使用前必须解包。如果文本字段中的值不代表有效的 Int
,toInt()
将 return nil
。如果使用 toInt()
转换,“2.1”、"seven" 和“”将全部 return nil
。我建议您使用可选绑定 (if let
) 语法来检查 nil
的转换,如果不是 nil
:
,则解包结果
if let yearOne = yearOneField.text.toInt() {
if let yearTwo = yearTwoField.text.toInt() {
if let yearThree = yearThreeField.text.toInt() {
// yearOne, yearTwo, and yearThree are all valid Ints
// so do the calculations
}
}
}
或者,如果您知道在无法将字段转换为 Int
时要使用默认值(如 0
),则可以使用 nil 合并运算符 ??
像这样:
let yearOne = yearOneField.text.toInt() ?? 0
let yearTwo = yearTwoField.text.toInt() ?? 0
let yearThree = yearThreeField.text.toInt() ?? 0
您可以像检查普通选项一样检查。
guard let unwrapped = myLabel else {return}
或者像这样
if myLabel == nil {
//do stuff
}
或者像这样:
if let unwrappedLabel = myLabel {
}
我使用 Swift 和 Xcode 6.1.1 编写了一个简单的应用程序。该程序是一个简单的计算器,运行良好,但我无法验证三个文本字段的非零输入。因此,如果用户将该字段留空然后点击 "calculate,",应用程序就会崩溃。
该应用接受三个输入,最初是字符串。我写了一个 if 语句来检查 nil 但它不起作用 - 无论如何它都会传递给 else。这是与我的问题相关的代码块:
...
@IBOutlet var calcButton: UIBarButtonItem!
@IBOutlet var yearOneField: UITextField!
@IBOutlet var yearTwoField: UITextField!
@IBOutlet var yearThreeField: UITextField!
@IBOutlet var displayWindow: UILabel!
@IBAction func calcButtonTapped(sender: AnyObject) {
if (yearOneField == nil) {
displayWindow.text = ("Non-zero entries are not permitted. Please enter new values.")
} else {
let yearOne = yearOneField.text.toInt()
let yearTwo = yearTwoField.text.toInt()
let yearThree = yearThreeField.text.toInt()
...
我想我可以评估 IBOutlet 为 nil,但那没有用。我是 Swift 和 Xcode 的新手,所以我希望这对那里更有经验的开发人员来说是一个 n00b 问题。谢谢。
文本字段本身永远不会是 nil
。它们是在初始化期间创建和分配的,您永远不会删除它们。
我想你想检查它们的 text
属性是否包含任何文本,你可以这样做:
已为 Swift 2 更新:
if let text = yearOneField.text where !text.isEmpty {
// perform the conversions
} else {
// the text field is empty
}
您可以使用 guard
:
guard let text = yearOneField.text where !text.isEmpty else {
// the text field is empty
return
}
// perform the conversions
我更喜欢 guard
语法,因为它更清楚 ideal 结果是什么。
如果您忘记在 Interface Builder 中连接它们,@IBOutlet
可能会是 nil
的唯一方法。通常您不需要检查,因为崩溃会告诉您解决该问题。
toInt()
函数 return 是一个可选的 Int
(又名 Int?
),在使用前必须解包。如果文本字段中的值不代表有效的 Int
,toInt()
将 return nil
。如果使用 toInt()
转换,“2.1”、"seven" 和“”将全部 return nil
。我建议您使用可选绑定 (if let
) 语法来检查 nil
的转换,如果不是 nil
:
if let yearOne = yearOneField.text.toInt() {
if let yearTwo = yearTwoField.text.toInt() {
if let yearThree = yearThreeField.text.toInt() {
// yearOne, yearTwo, and yearThree are all valid Ints
// so do the calculations
}
}
}
或者,如果您知道在无法将字段转换为 Int
时要使用默认值(如 0
),则可以使用 nil 合并运算符 ??
像这样:
let yearOne = yearOneField.text.toInt() ?? 0
let yearTwo = yearTwoField.text.toInt() ?? 0
let yearThree = yearThreeField.text.toInt() ?? 0
您可以像检查普通选项一样检查。
guard let unwrapped = myLabel else {return}
或者像这样
if myLabel == nil {
//do stuff
}
或者像这样:
if let unwrappedLabel = myLabel {
}