jeudi 13 décembre 2018

Objective C: A cleaner way to check if a string isn't empty before creating and assigning a string to a dictionary key?

I am creating an NSMutableDictionary and assigning an NSString (test and test1) to a parameter key.

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

    if (test.length) {
        dictionary[@"test"] = test;
    }

    if (test1.length) {
        dictionary[@"test1"] = test1;
    }

This method does work. However, I am going to eventually have more strings and don't want a bunch of if statements. I don't want the dictionary keys to exist if the string is empty or nil.

Not sure if there is a way around this.

I thought about creating a separate function that accepts a key string and string value and returns a dictionary if string value isn't empty.

Aucun commentaire:

Enregistrer un commentaire