且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么是Optional(“Text”) - Swift

更新时间:2022-06-13 23:10:45

textField的文本 是一个可选值,因为文本字段有时可能是空的。所以,使用如果让打开变量 ,请确保它不是nil - 否则你的程序可能会崩溃:

The text of your textField is an optional value because a text field can sometimes be empty. So, use an if let to unwrap the variable and make sure it's not nil — otherwise your program could crash:

if let text: String = textField.text {
   textLabel.text = text
}