I have the following line of code:
var info = [String : Any]()
Later i need to compare if info
is not nil / set:
Something like this:
if(info != nil){
InfoCenter.default().info = info
}
But XCode warns: Comparing non-optional value of type '[String : Any]' to nil always returns true
How can I fix this?
Thanks for any help.
You can check if the Dictionary is empty.
if !info.isEmpty {
InfoCenter.default().info = info
}
If you're expecting info
to be nil
, you must declare it as an optional Dictionary
like,
var info: [String:Any]?
Now, use if-let
to get the value of info
and check the elements in it using isEmpty
, ie
if let info = info, !info.isEmpty {
InfoCenter.default().info = info
}
By using following
var info = [String : Any]()
Means you have initialized info object, Which means its not nil it have a empty value. So swift compiler trying to tell you that info object will never be nil according to above declaration. To remove this warning you can do use optional binding.
var info :[String : Any]?
**OR**
var info :[String : Any]!
Try this
if info.count != 0
{
InfoCenter.default().info = info
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.