如何安全地从用户默认值中解包一个值?
How to safely unwrap a value from user defaults?
if let seen: Bool = defaults.bool(forKey: UtilitiesKeys.mainTutorialSeen) {
return seen
}
return false
如果我这样做 swift 显示一个问题:
Conditional cast from 'Bool' to 'Bool' always succeeds, Non-optional
expression of type 'Bool' used in a check for optionals.
因为我的密钥可能没有值,所以我不想强制解包。现在显然它是这样工作的,但是我如何安全地解开价值而不 swift 抱怨?
我也试过用guard...
if let seen = UserDefaults.standard.object(forKey: UtilitiesKeys.mainTutorialSeen)) as? Bool {
return seen
}else {
return false
}
如文档中所建议:
-boolForKey:
is equivalent to -objectForKey:
, except that it converts the returned value to a BOOL
. If the value is an NSNumber
, NO
will be returned if the value is 0
, YES
otherwise. If the value is an NSString
, values of "YES"
or "1"
will return YES
, and values of "NO"
, "0"
, or any other string will return NO
. If the value is absent or can't be converted to a BOOL
, NO
will be returned.
open func bool(forKey defaultName: String) -> Bool
它不再是可选的。所以你不需要用 if let
.
来转换它
您可以直接使用:
let seen = defaults.bool(forKey: UtilitiesKeys.mainTutorialSeen)
Apple 文档说:
/*!
-boolForKey: is equivalent to -objectForKey:, except that it converts the returned value to a BOOL. If the value is an NSNumber, NO
will be returned if the value is 0, YES otherwise. If the value is an
NSString, values of "YES" or "1" will return YES, and values of "NO",
"0", or any other string will return NO. If the value is absent or
can't be converted to a BOOL, NO will be returned.
open func bool(forKey defaultName: String) -> Bool
条件绑定的初始化器必须是 Optional 类型,而不是 'Bool'
希望对您有所帮助。
if let seen: Bool = defaults.bool(forKey: UtilitiesKeys.mainTutorialSeen) {
return seen
}
return false
如果我这样做 swift 显示一个问题:
Conditional cast from 'Bool' to 'Bool' always succeeds, Non-optional expression of type 'Bool' used in a check for optionals.
因为我的密钥可能没有值,所以我不想强制解包。现在显然它是这样工作的,但是我如何安全地解开价值而不 swift 抱怨?
我也试过用guard...
if let seen = UserDefaults.standard.object(forKey: UtilitiesKeys.mainTutorialSeen)) as? Bool {
return seen
}else {
return false
}
如文档中所建议:
-boolForKey:
is equivalent to-objectForKey:
, except that it converts the returned value to aBOOL
. If the value is anNSNumber
,NO
will be returned if the value is0
,YES
otherwise. If the value is anNSString
, values of"YES"
or"1"
will returnYES
, and values of"NO"
,"0"
, or any other string will returnNO
. If the value is absent or can't be converted to aBOOL
,NO
will be returned.
open func bool(forKey defaultName: String) -> Bool
它不再是可选的。所以你不需要用 if let
.
您可以直接使用:
let seen = defaults.bool(forKey: UtilitiesKeys.mainTutorialSeen)
Apple 文档说:
/*! -boolForKey: is equivalent to -objectForKey:, except that it converts the returned value to a BOOL. If the value is an NSNumber, NO will be returned if the value is 0, YES otherwise. If the value is an NSString, values of "YES" or "1" will return YES, and values of "NO", "0", or any other string will return NO. If the value is absent or can't be converted to a BOOL, NO will be returned.
open func bool(forKey defaultName: String) -> Bool
条件绑定的初始化器必须是 Optional 类型,而不是 'Bool'
希望对您有所帮助。