在函数内部使用关键字 guard 是否会自动使函数的参数在 swift 中成为可选参数?
Does using the keyword guard inside a function automatically make a function's parameter optional in swift?
我是编程新手,正在阅读 Start Developing iOS Apps (Swift) programming guide。我知道可选项有一个问号后缀,这意味着该值可以是 nil 或其他一些值。 Apple 的指南正在指导我们如何使用 UIImagePickerController
及其委托。 Apple 使用委托方法 imagePickerControllerdidFinishPickingMediaWithInfo(_:)
,它有一个字典作为参数。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
}
Apple's UIImagePickerControllerDelegate documentation 未将此参数列为可选参数,我们为本教程创建的 UIImagePickerController
实例也不是可选参数,但教程说明如下:
"This code accesses the original, unedited image from the info
dictionary. It safely unwraps the optional returned by the dictionary
and casts it as a UIImage object. The expectation is that the
unwrapping and casting operations will never fail. If they do, it
represents either a bug in your app that needs to be fixed at design
time."
这是否意味着通过使用 guard 关键字,它会自动将变量变为可选变量?
根据 Apple 文档
A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
和
The value of any condition in a guard statement must be of type Bool or a type bridged to Bool. The condition can also be an optional binding declaration.
所以guard
保证变量总是包含一个值,否则控制将被转移到范围之外。
It does not make any other variable optional.
一句话,没有
guard 语句断言某个布尔表达式为真,或者强制退出当前作用域。它通常与 let 一起使用来解包一个可选的,如果可选包含一个值则继续或者如果它是 nil 则退出但它不必这样做。您也可以使用这样的代码:
guard x == 3 else {
return
}
或计算结果为布尔值 (true/false) 的任何表达式。
没有。可选值源于尝试从字典中检索值。虽然 info
参数是 non-optional 类型 [String: Any]
的字典,但下标引用 info[UIImagePickerControllerOriginalImage]
returns 是 Any?
类型的可选值。
来自 The Swift Programming Language (Swift 3.0.1)、"Accessing and Modifying a Dictionary":
Because it is possible to request a key for which no value exists, a dictionary’s subscript returns an optional value of the dictionary’s value type. If the dictionary contains a value for the requested key, the subscript returns an optional value containing the existing value for that key. Otherwise, the subscript returns nil
:
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
// Prints "The name of the airport is Dublin Airport."
我是编程新手,正在阅读 Start Developing iOS Apps (Swift) programming guide。我知道可选项有一个问号后缀,这意味着该值可以是 nil 或其他一些值。 Apple 的指南正在指导我们如何使用 UIImagePickerController
及其委托。 Apple 使用委托方法 imagePickerControllerdidFinishPickingMediaWithInfo(_:)
,它有一个字典作为参数。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
}
Apple's UIImagePickerControllerDelegate documentation 未将此参数列为可选参数,我们为本教程创建的 UIImagePickerController
实例也不是可选参数,但教程说明如下:
"This code accesses the original, unedited image from the info dictionary. It safely unwraps the optional returned by the dictionary and casts it as a UIImage object. The expectation is that the unwrapping and casting operations will never fail. If they do, it represents either a bug in your app that needs to be fixed at design time."
这是否意味着通过使用 guard 关键字,它会自动将变量变为可选变量?
根据 Apple 文档
A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
和
The value of any condition in a guard statement must be of type Bool or a type bridged to Bool. The condition can also be an optional binding declaration.
所以guard
保证变量总是包含一个值,否则控制将被转移到范围之外。
It does not make any other variable optional.
一句话,没有
guard 语句断言某个布尔表达式为真,或者强制退出当前作用域。它通常与 let 一起使用来解包一个可选的,如果可选包含一个值则继续或者如果它是 nil 则退出但它不必这样做。您也可以使用这样的代码:
guard x == 3 else {
return
}
或计算结果为布尔值 (true/false) 的任何表达式。
没有。可选值源于尝试从字典中检索值。虽然 info
参数是 non-optional 类型 [String: Any]
的字典,但下标引用 info[UIImagePickerControllerOriginalImage]
returns 是 Any?
类型的可选值。
来自 The Swift Programming Language (Swift 3.0.1)、"Accessing and Modifying a Dictionary":
Because it is possible to request a key for which no value exists, a dictionary’s subscript returns an optional value of the dictionary’s value type. If the dictionary contains a value for the requested key, the subscript returns an optional value containing the existing value for that key. Otherwise, the subscript returns
nil
:if let airportName = airports["DUB"] { print("The name of the airport is \(airportName).") } else { print("That airport is not in the airports dictionary.") } // Prints "The name of the airport is Dublin Airport."