Swift 中的函数参数如何强制解包?
How force unwrapping works in function parameters in Swift?
我在 Swift
中有一个功能,例如,
func getValue(param: String!) -> String {
//access param
//return a string
}
来电就像,
let retVal = getValue(param: nil)
我预计参数会在接受函数参数时尝试强制解包并崩溃。但是代码没有崩溃。谁能告诉我 我错过了什么?
签名的情况下会有什么不同,
func getValue(param: String?) -> String
此处!
表示参数将保存字符串值或nil
。因此,当您将 nil
分配给 !
时,它不应该崩溃。
但是当您尝试访问该值时,它会崩溃。
您的代码中的这种修改会导致崩溃,因为我们正在尝试访问变量中的值 nil
。
func getValue(param: String!) -> String {
if param.isEmpty {
return ""
}
return ""
}
let retVal = getValue(param: nil)
另一个例子:
var string: String!
string = nil //This will not crash, but after this line if you try to access or perform any operation on it, it will
关于这个func getValue(param: String?)
:
在这种情况下,param
将是 optional type
,这与上述情况不同。所以在这里你必须打开它。
隐式展开的可选(或 IUO)可能仍包含 nil
作为值!在 IUO 的情况下,只有当你在它上面调用任何 method/variable(例如 param.count
)或者当你将它分配给一个 NON-Optional 变量时,你才会崩溃,这将 导致强制展开操作:
var param: String! = nil
var nonOptionalStr: String = param //<- Crash because it will try to force-unwrap param
崩溃的另一个例子:
func foo(param: String!) -> String {
return param //<- Crash! As return type is non-optional String it will try to force unwrap the param.
}
另一方面 IUO(以及Optionals)也是Any
类型所以如果你传递一个包含nil的String!
给一个方法预计 Any
它不会崩溃,因为不会有强制解包操作,因为 Any?
可以很容易地转换为 Any
。
let str: String! = nil
print(str) //<- No Crash! Becaise print accepts Any, so there will be no force-unwrapping
//Output: nil
类型为 Any
的另一个示例:
var a: Any? = nil
var b: Any = a //<- No Crash! As (Any?) is kind of (Any), though you will get a warning from compiler: Expression implicitly coerced from 'Any?' to 'Any'
print(b)
//Output: nil
我在 Swift
中有一个功能,例如,
func getValue(param: String!) -> String {
//access param
//return a string
}
来电就像,
let retVal = getValue(param: nil)
我预计参数会在接受函数参数时尝试强制解包并崩溃。但是代码没有崩溃。谁能告诉我 我错过了什么?
签名的情况下会有什么不同,
func getValue(param: String?) -> String
此处!
表示参数将保存字符串值或nil
。因此,当您将 nil
分配给 !
时,它不应该崩溃。
但是当您尝试访问该值时,它会崩溃。
您的代码中的这种修改会导致崩溃,因为我们正在尝试访问变量中的值 nil
。
func getValue(param: String!) -> String {
if param.isEmpty {
return ""
}
return ""
}
let retVal = getValue(param: nil)
另一个例子:
var string: String!
string = nil //This will not crash, but after this line if you try to access or perform any operation on it, it will
关于这个func getValue(param: String?)
:
在这种情况下,param
将是 optional type
,这与上述情况不同。所以在这里你必须打开它。
隐式展开的可选(或 IUO)可能仍包含 nil
作为值!在 IUO 的情况下,只有当你在它上面调用任何 method/variable(例如 param.count
)或者当你将它分配给一个 NON-Optional 变量时,你才会崩溃,这将 导致强制展开操作:
var param: String! = nil
var nonOptionalStr: String = param //<- Crash because it will try to force-unwrap param
崩溃的另一个例子:
func foo(param: String!) -> String {
return param //<- Crash! As return type is non-optional String it will try to force unwrap the param.
}
另一方面 IUO(以及Optionals)也是Any
类型所以如果你传递一个包含nil的String!
给一个方法预计 Any
它不会崩溃,因为不会有强制解包操作,因为 Any?
可以很容易地转换为 Any
。
let str: String! = nil
print(str) //<- No Crash! Becaise print accepts Any, so there will be no force-unwrapping
//Output: nil
类型为 Any
的另一个示例:
var a: Any? = nil
var b: Any = a //<- No Crash! As (Any?) is kind of (Any), though you will get a warning from compiler: Expression implicitly coerced from 'Any?' to 'Any'
print(b)
//Output: nil