1.Wrapped 意味着您将值放在一个变量中,该变量可能为空。例如:
let message: String? // message can be nil
message = "Hello World!" // the value "Hello World!" is wrapped inside the message variable.
print(message) // print the value that is wrapped in message - it can be null.
2.Unwrap的意思是你得到变量的值来使用它。
textField?.text = "Hello World!" // you get the value of text field and set text to "Hello World!" if textField is not nil.
textField!.text = "Hello World!" // force unwrap the value of text field and set text to "Hello World!" if text field is not nil, other wise, the application will crash. (you want to make sure that textField muse exists).
3.Optional:是一个可以为nil的变量。当你使用它的值时,你需要明确地解开它:
let textField: UITextField? // this is optional
textField?.text = "Hello World" // explicitly tells the compiler to unwrap it by putting an "?" here
textField!.text = "Hello World" // explicitly tells the compiler to unwrap it by putting in "!" here.
隐式展开 optional(?) 是一个可选项(值可以是 nil)。但是当你使用它时,你不必告诉编译器解包它。它将被强制隐式解包(默认)
let textField: UITextField!
textField.text = "Hello World!" // it will forced unwrap the variable and the program will crash if the textField is nil.
4.如果您认为该值可以为零,请尝试在大多数情况下始终使用 optional(?)。仅当您 100% 确定变量在使用时不能为 nil 时才使用隐式展开的 optional(!)(但您不能在 Class 构造函数中设置它)。
5.Implicitly 表示自动,你不必告诉编译器,它会自动执行,这很糟糕,因为有时你不知道你正在解开一个可能导致程序崩溃的可选项。在编程中,显式总是比隐式好。