Check for Nil in Dictionary

Check for nil in dictionary

The way of unwrapping objects of type Any that contain optionals is kind of weird but you can check that the values aren't nil in your mirror like this:

for x in Mirror(reflecting: cl).children {    
if case Optional<Any>.some(let val) = x.value {
print(type(of: val))
colorDict[x.label!] = val
}
}

How to check if NSDictionary is not nil in Swift 2

The error is due to assuming (force casting) a value that can sometimes be nil. Swift is awesome, because it allows conditional unwraps and conditional casts in very concise statements. I recommend the following (for Swift 1-3):

Use "if let" to conditionally check for "action" in the dictionary.

Use as? to conditionally cast the value to a String

if let actionString = val["action"] as? String {
// action is not nil, is a String type, and is now stored in actionString
} else {
// action was either nil, or not a String type
}

Check Dictionary to containing nil values and empty values

Change the type of Dictionary from [String:Any] to [String:Any?] to compare values to nil

let someDict = ["first":"name","second":1,"third":1.2,"someNilValue":nil] as [String:Any?]

func checkEmptyDict(_ dict:[String:Any?]) -> Bool {

for (_,value) in dict {
if value == nil || value as? String == "" { return true }
}

return false

}

checkEmptyDict(someDict) //returns true

Test for nil values presence in Dictionary

Use contains(where:) on the dictionary values:

// Enable button if at least one value is not nil:
button.isEnabled = dict.values.contains(where: { $0 != nil })

Or

// Enable button if no value is nil:
button.isEnabled = !dict.values.contains(where: { $0 == nil })

check for nil and extract data from dictionary in swift

The error in guard statement is because the method getDateString: not returns an optional value. you are checking the return value of that method which will never be nil as per definition. You need to check date value from dict as it is optional.

try like,

var fromDate = "None" 
if let dateVal = dict["fromDate"] as? Date {
fromDate = getDateString(createdDate: dateVal)
cell.fromDateLabel.text = getTimeStringISO8601(createdDate: fromDate)
} else {
cell.fromDateLabel.text = fromDate
}

Check if NSDictionary is Null?

It looks like you have a dangling or wild pointer.

You can consider Objective-C objects as pointers to structs.

You can then of course compare them with NULL, or with other pointers.

So:

( myDict == NULL )

and

( myDict == [ NSNull null ] )

are both valid.

The first one will check if the pointer is NULL. NULL is usually defined as a void * with a value of 0.

Note that, for Objective-C objects, we usually use nil. nil is also defined as a void * with a value of 0, so it equals NULL. It's just here to denote a NULL pointer to an object, rather than a standard pointer.

The second one compares the address of myDict with the singleton instance of the NSNull class. So you are here comparing two pointers values.

So to quickly resume:

NULL == nil == Nil == 0

And as [ NSNull null ] is a valid instance:

NULL != [ NSNull null ]

Now about this:

( [ myDict count ] == 0 )

It may crash if you have a wild pointer:

NSDictionary * myDict;

[ myDict count ];

Unless using ARC, it will surely crash, because the myDict variable has not been initialised, and may actually point to anything.

It may also crash if you have a dangling pointer:

NSDictionary * myDict;

myDict = [ [ NSDictionary alloc ] init ];

[ myDict release ];
[ myDict count ];

Then you'll try to send a message to a deallocated object.

Sending a message to nil/NULL is always valid in Objective-C.

So it depends if you want to check if a dictionary is nil, or if it doesn't have values (as a valid dictionary instance may be empty).

In the first case, compare with nil. Otherwise, checks if count is 0, and ensure you're instance is still valid. Maybe you just forgot a retain somewhere.

Elegantly populate dictionary from a struct checking nil values

It's usually not a good idea to have a dictionary with a value that is optional. Dictionaries use the assignment of nil as an indication that you want to delete a key/value pair from the dictionary. Also, dictionary lookups return an optional value, so if your value is optional you will end up with a double optional that needs to be unwrapped twice.

You can use the fact that assigning nil deletes a dictionary entry to build up a [String : String] dictionary by just assigning the values. The ones that are nil will not go into the dictionary so you won't have to remove them:

struct A {
var first: String?
var second: String?
var third: String?
}

let a = A(first: "one", second: nil, third: "three")

let pairs: [(String, String?)] = [
("first", a.first),
("second", a.second),
("third", a.third)
]

var dictionary = [String : String]()

for (key, value) in pairs {
dictionary[key] = value
}

print(dictionary)
["third": "three", "first": "one"]

As @Hamish noted in the comments, you can use a DictionaryLiteral (which internally is just an array of tuples) for pairs which allows you to use the cleaner dictionary syntax:

let pairs: DictionaryLiteral<String,String?> = [
"first": a.first,
"second": a.second,
"third": a.third
]

All of the other code remains the same.

Note: You can just write DictionaryLiteral and let the compiler infer the types, but I have seen Swift fail to compile or compile very slowly for large dictionary literals. That is why I have shown the use of explicit types here.


Alternatively, you can skip the Array or DictionaryLiteral of pairs and just assign the values directly:

struct A {
var first: String?
var second: String?
var third: String?
}

let a = A(first: "one", second: nil, third: "three")

var dictionary = [String : String]()

dictionary["first"] = a.first
dictionary["second"] = a.second
dictionary["third"] = a.third

print(dictionary)
["third": "three", "first": "one"]


Related Topics



Leave a reply



Submit