Skip to content

Instantly share code, notes, and snippets.

View apradanas's full-sized avatar

Aditya Pradana Sugiarto apradanas

  • Yogyakarta, Indonesia
View GitHub Profile
@apradanas
apradanas / gist:b68ccf066966d8d0ebfe
Last active October 5, 2015 07:45
Replace null (NSNull) with empty string in object

If you are dealing with an unstable API, you may want to iterate through all the keys to check for null. This will prevent crash when saving the object as well.

+ (id)replaceNullWithEmptyStringForObject:(id)object {
    if ([object isKindOfClass:[NSArray class]]) {
        NSMutableArray *mutableArray = [object mutableCopy];
        [mutableArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [mutableArray setObject:[self replaceNullWithEmptyStringForObject:obj] atIndexedSubscript:idx];
        }];