0

I have a Dictionary that contains values in such a way that some values are empty string.

let fbPostDic: [String: AnyObject] = [
    "title":"",
    "first_name”:”Ali“,
    "last_name”:”Ahmad“,
    "nick_name”:”ahmad”,
    "gender":1,
    "gender_visibility":2,
    "dob":"1985-08-25",
    "dob_visibility":2,
    "city":"Lahore",
    "city_visibility":2,
    "bio”:”Its bio detail.”,
    "bio_visibility":2,
    "nationality":"Pakistan",
    "nationality_visibility":2,
    "relationship_status”:”Single”,
    "rel_status_visibility":2,
    "relation_with":"",
    "relation_with_visibility":2,
    "social_media_source":"Facebook",
]

I want to filter this dictionary such a way that new dictionary should just contains elements without empty strings.

let fbPostDic: [String: AnyObject] = [
    "first_name”:”Ali“,
    "last_name”:”Ahmad“,
    "nick_name”:”ahmad”,
    "gender":1,
    "gender_visibility":2,
    "dob":"1985-08-25",
    "dob_visibility":2,
    "city":"Lahore",
    "city_visibility":2,
    "bio”:”Its bio detail.”,
    "bio_visibility":2,
    "nationality":"Pakistan",
    "nationality_visibility":2,
    "relationship_status”:”Single”,
    "rel_status_visibility":2,
    "relation_with_visibility":2,
    "social_media_source":"Facebook",
]

There are present ways like

let keysToRemove = dict.keys.array.filter { dict[$0]! == nil }

But its support iOS9.0 or above. I want to give support of iOS8.0 as well.

Any suggestions?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
msmq
  • 1,298
  • 16
  • 28
  • 1
    I want such a method which also works on iOS8. dict.keys.array.filter method support iOS9.0 or above. I just referenced swift because currently working on swift. – msmq Feb 05 '16 at 12:23
  • 1
    Actually, i am not confusing Swift 1 and Swift 2; ObjectiveC solution will also work. I just want a solution that woks on iOS8.0 and above. Sorry for misunderstanding. – msmq Feb 05 '16 at 12:30
  • http://stackoverflow.com/questions/12213822/replace-occurance-of-nsnull-in-nested-nsdictionary – Vizllx Feb 05 '16 at 12:47

2 Answers2

1

Because the above dict is a constant, add an extra init method in Dictionary extension can simplify the process:

extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
    init(_ elements: [Element]){
        self.init()
        for (key, value) in elements {
            self[key] = value
        }
    }
} 
print(Dictionary(dict.filter { $1 as? String != "" }))

However, if above dict can be declared as a variable. Probably try the one below without above extra Dictionary extension:

var dict: [String : AnyObject] = [...]
dict.forEach { if $1 as? String == "" { dict.removeValueForKey($0) } }
print(dict)
Allen
  • 1,714
  • 17
  • 19
0

This solution will also work; But Allen's solution is more precise.

public class func filterDictionaryFromEmptyString(var dictionary:[String: AnyObject]) -> [String: AnyObject]
{
    print(dictionary.keys.count)
    for key:String in dictionary.keys
    {
        print(key)
        print(dictionary[key]!)
        if String(dictionary[key]!) == ""
        {
            dictionary.removeValueForKey(key)
        }
    }
    print(dictionary.keys.count)
    return dictionary
}
msmq
  • 1,298
  • 16
  • 28