1

I'm trying to add some values with keys in my dictionary for posting request to server. I have no issues on ios7 but having issue while creating dictionary in ios5, when a null value enters it doesnt insert that key or thereafter in the dictionary. This is a sample of my code

NSLog(@"name:%@, address:%@,city:%@, state:%@, country:%@, postal:%@ ,phone:%@,age:%@, gender:%@",self.pop.firstNameTextField.text,self.pop.addressTextField.text, self.pop.cityTextField.text,[NSNumber numberWithInt:stateId],[NSNumber numberWithInt:countryId],self.pop.postalCodeTextField.text,self.pop.phoneNumberTextField.text,self.pop.ageTextField.text,self.pop.genderTextField.text);


NSDictionary *details = [NSDictionary dictionaryWithObjectsAndKeys:
                          self.pop.firstNameTextField.text,@"name",
                          self.pop.addressTextField.text,@"address",
                          self.pop.cityTextField.text,@"city",
                          [NSNumber numberWithInt:stateId],@"state",
                          [NSNumber numberWithInt:countryId],@"country",
                          self.pop.postalCodeTextField.text,@"postalCode",
                          self.pop.phoneNumberTextField.text,@"phoneNo",
                          self.pop.ageTextField.text,@"age",
                          self.pop.genderTextField.text,@"gender",
                          nil];


NSLog(@"MyDetails:%@",details);

Log result in ios 7:

name: GamerLegend,address:india,city:, state:1, country:1, postal: ,phone:, age:, gender:
MyDetails:
{
    address = India;
    age = "";
    city = "";
    country = 1;
    gender = "";
    name = "GamerLegend";
    phoneNo = "";
    postalCode = "";
    state = 1;
}

Same Log result in ios5:

name: GamerLegend,address:haripad,city:(null), state:1, country:1, postal:(null) ,phone:(null), age:(null), gender:(null)
MyDetails:
{
    address = India;
    name = "GamerLegend";
}

I want the log of ios5 same as that of ios7. I did some search and I know I can compare the value beforehand to check if its null and then add to dictionary with empty string. But that will be so tedious. I was wondering if there is any other simple workaround for this. Is there any better solution?

This is my current solution

-(void) checkForNull
{

    if([self.pop.firstNameTextField.text length] == 0)
        self.pop.firstNameTextField.text=@"";

    if([self.pop.addressTextField.text length] == 0)
        self.pop.addressTextField.text=@"";

    if([self.pop.cityTextField.text length] == 0)
        self.pop.cityTextField.text=@"";

    if([self.pop.postalCodeTextField.text length] == 0)
        self.pop.postalCodeTextField.text=@"";

    if([self.pop.phoneNumberTextField.text length] == 0)
        self.pop.phoneNumberTextField.text=@"";

    if([self.pop.ageTextField.text length] == 0)
        self.pop.ageTextField.text=@"18";

    if([self.pop.genderTextField.text length] == 0)
        self.pop.genderTextField.text=@"";
}
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • You can't add nil/null to a dictionary. "An NSInvalidArgumentException is raised if a key or value object is nil." https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-CBHCIFDD – Tim Oct 25 '13 at 09:32
  • Try with change your condition if([self.pop.firstNameTextField.text length] == 0) by if([self.pop.firstNameTextField.text length] == 0 || [self.pop.firstNameTextField.text isEqualToString:@"(null)"]) – Pradhyuman sinh Oct 25 '13 at 10:46

1 Answers1

0

This should work if it fits your requirements:

Create an array of EXPECTED keys. Then you loop through this and compare this with the output of the allKeys method in your dictionary. If a key is missing there you could add the missing key with an empty string, [NSNull null], or whatever floats your boat. (You would need to make the dictionary mutable of course).

Something like this:

for (NSString *expectedKey in allExpectedKeys) {
    BOOL keyMissing = YES;
    for (NSString *existingKey in [dictionary allKeys]) {

        if ([expectedKey isEqualToString:existingKey]) {
            keyMissing = NO;
            break;
        }
    }
    if (keyMissing) {
        [dictionary setObject:[NSNull null] forKey:expectedKey];
    }
}
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32