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=@"";
}