Your dictionary option is probably not zero. Probably the problem is that your dictionary does not contain a value for the "action" key.
When you say val["action"] , the dictionary (being NSDictionary ) returns Optional<AnyObject> . If val contains the "action" key, it returns Some(value) . If val does not contain the "action" key, it returns None , which is zero.
You can expand Optional on your cast and choose the course of action based on whether it was null using the if-let :
if let action = val["action"] as? String {
If you really think that val itself may be nil, you need to declare your function this way, and you can unpack val without renaming using the somewhat confusing guard statement:
func doSmth(val: NSDictionary?) { guard let val = val else { // If val vas passed in as nil, I get here. return } // val is now an NSDictionary, not an Optional<NSDictionary>. ... }
rob mayoff
source share