If I have a Dictionary returned from a NSNotification containing the following
print(notificationObj.object)
Optional({
age = "<null>";
names = (
David
);
})
Then the guard else is called when trying to assign this to a variable:
guard let categories = notificationObj.object as? [String:[String]] else {
// Gets to here
return
}
How can I handle the case where a Dictionary key is null.
Your dictionary does contain ...
Optional({
age = "<null>";
names = (
David
);
})
... and ...
age = ...
is String = String
(value is single String
),names = ( ... )
is String = [String]
(value is array of String
s).You can't cast it to [String:[String]]
because the first pair doesn't fit this type. This is the reason why your guard
statement hits else
.
Hard to answer your question. Dictionary contains names
, you want categories
, names
key does contain David
, which doesn't look like category, ... At least you know why guard
hits else
.