Detect a Null value in NSDictionary

You can use the as? operator, which returns an optional value (nil if the downcast fails) if let latestValue = sensor[“latestValue”] as? String { cell.detailTextLabel.text = latestValue } I tested this example in a swift application let x: AnyObject = NSNull() if let y = x as? String { println(“I should never be printed: \(y)”) … Read more

Replace occurrences of NSNull in nested NSDictionary

A small modification to the method can make it recursive: @interface NSDictionary (JRAdditions) – (NSDictionary *) dictionaryByReplacingNullsWithStrings; @end @implementation NSDictionary (JRAdditions) – (NSDictionary *) dictionaryByReplacingNullsWithStrings { const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: self]; const id nul = [NSNull null]; const NSString *blank = @””; for (NSString *key in self) { const id object = [self … Read more

Replace all NSNull objects in an NSDictionary

I’ve made a few changes to Jacob’s original answer to extend it to handle dictionaries and arrays stored within the original dictionary. #import “NSDictionary+NullReplacement.h” #import “NSArray+NullReplacement.h” @implementation NSDictionary (NullReplacement) – (NSDictionary *)dictionaryByReplacingNullsWithBlanks { const NSMutableDictionary *replaced = [self mutableCopy]; const id nul = [NSNull null]; const NSString *blank = @””; for (NSString *key in self) … Read more