Last active
August 31, 2015 23:58
-
-
Save demonar/bbb10a4635f0694d2bc4 to your computer and use it in GitHub Desktop.
Swift Static method used to remove nulls from dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class DictionaryUtils { | |
class func removeNullsFromDictionary(origin:[String:AnyObject]) -> [String:AnyObject] { | |
var destination:[String:AnyObject] = [:] | |
for key in origin.keys { | |
if origin[key] != nil && !(origin[key] is NSNull){ | |
if origin[key] is [String:AnyObject] { | |
destination[key] = self.removeNullsFromDictionary(origin[key] as! [String:AnyObject]) | |
} else if origin[key] is [AnyObject] { | |
let orgArray = origin[key] as! [AnyObject] | |
var destArray: [AnyObject] = [] | |
for item in orgArray { | |
if item is [String:AnyObject] { | |
destArray.append(self.removeNullsFromDictionary(item as! [String:AnyObject])) | |
} else { | |
destArray.append(item) | |
} | |
} | |
} else { | |
destination[key] = origin[key] | |
} | |
} else { | |
destination[key] = "" | |
} | |
} | |
return destination | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment