简体   繁体   English

将 'Bool' 类型的非可选值与 'nil' 进行比较总是返回 true

[英]Comparing non-optional value of type 'Bool' to 'nil' always returns true

I have an if-else statement where I am checking if the value coming from user defaults is nil or not like this:我有一个 if-else 语句,我正在检查来自用户默认值的值是否为 nil,如下所示:

 if defaults.bool(forKey: "abcd") != nil{
       //Do something
    }
    else{
        //do something else
    }

But Xcode is giving me an error saying: "Comparing non-optional value of type 'Bool' to 'nil' always returns true"但是 Xcode 给了我一个错误说:“将'Bool'类型的非可选值与'nil'进行比较总是返回true”

Can someone explain what's happening here and how to fix this?有人可以解释这里发生了什么以及如何解决这个问题吗?

bool(forKey:) returns a NON-optional, which cannot be nil. bool(forKey:)返回一个非可选的,不能为 nil。 If the key is missing in the user defaults, the return value will be false .如果用户默认值中缺少该键,则返回值为false

If you want trinary logic here (nil/true/false) use object(forKey:) looking for an NSNumber, and if present, take its boolValue .如果你想要这里的三元逻辑(nil/true/false)使用object(forKey:)寻找一个 NSNumber,如果存在,取它的boolValue

As作为

defaults.bool(forKey: "abcd")

will return false by default check Docs , so it will never be optional默认情况下将返回false检查Docs ,因此它永远不会是可选的

The Boolean value associated with the specified key.与指定键关联的 Boolean 值。 If the specified key doesn't exist, this method returns false.如果指定的键不存在,则此方法返回 false。

The func bool(forKey: "abcd") returns Bool type not optional. func bool(forKey: "abcd") 返回 Bool 类型不是可选的。

Which means you cant compare it to bool, what you can do is simply:这意味着您不能将其与 bool 进行比较,您可以做的很简单:

 if defaults.bool(forKey: "abcd") {
   //Do something
} else {
    //do something else
}

Now if the key exists and has true value it will get into the if statement, if it does not exists or is false it will go to the else.现在,如果键存在并且具有真值,它将进入 if 语句,如果它不存在或为假,它将 go 到 else。

If you have any doubts you can read about the func in the following Apple developer link: Apple:bool(forKey:)如果您有任何疑问,可以在以下 Apple 开发人员链接中阅读有关 func 的信息: Apple:bool(forKey:)

Objective-c property in swift. swift 中的 Objective-c 属性。 If you're using some objective c property in swift and it says something like "Comparing non-optional value of type 'XYZ' to 'nil' always returns true" you have to make that objective c property to "_Nullable" so that property may not be optional anymore. If you're using some objective c property in swift and it says something like "Comparing non-optional value of type 'XYZ' to 'nil' always returns true" you have to make that objective c property to "_Nullable" so that property可能不再是可选的了。 Like @property (strong,nonatomic) NSString *_Nullable someString;@property (strong,nonatomic) NSString *_Nullable someString;

defaults.bool(forKey: "abcd") != nil

The first part, defaults.bool(forKey: "abcd") , returns a non-optional boolean.第一部分defaults.bool(forKey: "abcd")返回一个非可选的 boolean。 We know that because bool(forKey:) returns Bool , not Bool?我们知道,因为bool(forKey:)返回Bool ,而不是Bool? . . Therefore, you'll always get a Bool value, ie either true or false , never nil .因此,您将始终获得一个Bool值,即truefalse ,绝不是nil Note the documentation:注意文档:

If the specified key doesn't exist, this method returns false.如果指定的键不存在,则此方法返回 false。

"Comparing non-optional value of type 'Bool' to 'nil' always returns true" “将 'Bool' 类型的非可选值与 'nil' 进行比较总是返回 true”

The compiler is simply pointing out that it knows that defaults.bool(forKey: "abcd") can't be nil , and since you're comparing it to nil , you're probably making a mistake.编译器只是指出它知道defaults.bool(forKey: "abcd")不能为nil ,并且由于您将其与nil进行比较,因此您可能犯了一个错误。 Your else block will never execute.你的else块永远不会执行。

Can someone explain what's happening here and how to fix this?有人可以解释这里发生了什么以及如何解决这个问题吗?

It depends on what you mean for the code to do.这取决于您对代码执行的操作。 If you want to take different actions depending on whether the value is true or false , then compare it to one of those values.如果您想根据值是true还是false采取不同的操作,请将其与这些值之一进行比较。 If you want to get an optional value back, use object(forKey:) (which returns an optional) instead.如果您想取回一个可选值,请改用object(forKey:) (它返回一个可选值)。

i solved like this:我这样解决:

if(Userdefaults.standart.bool(forkey: "blablabool"){

}

This works.. When you call this if its null it returns false.这有效.. 当你调用它时,如果它的 null 它返回 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“JSON”类型的非可选值与“nil”进行比较总是返回 true。 有什么建议可以解决这个问题吗? - Comparing non-optional value of type 'JSON' to 'nil' always returns true. Any suggestion to fix this? 检查 NSSize 是否为零 - 比较“NSSize”类型的非可选值? - Check if NSSize is nil - Comparing non-optional value of type 'NSSize'? 将非可选的 Any 与 nil 进行比较总是错误的? - Comparing non-optional Any to nil is always false? 无法强制展开非可选类型“Bool”的值 - Cannot force unwrap value of non-optional type 'Bool' Swift:测试非可选值是否为零/未设置 - Swift: test if a non-optional value is nil / not set 致命错误:解开可选值时意外发现nil(无法强制解开非可选类型'String'的值) - fatal error: unexpectedly found nil while unwrapping an Optional value (cannot force unwrap value of non-optional type 'String') 无法强制展开非可选类型的值,但打印“可选” - Cannot force unwrap value of non-optional type but prints with "optional" 不能对“Auth”类型的非可选值使用可选链 - Cannot use optional chaining on non-optional value of type 'Auth' 不能对类型为“[Int]”的非可选值使用可选链 - Cannot use optional chaining on non-optional value of type '[Int]' 不能对“ViewController”类型的非可选值使用可选链接 - Cannot use optional chaining on non-optional value of type 'ViewController'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM