我正在尝试在我的字典中添加一些带有键的值,以将请求发布到服务器。我在 ios7 上没有问题,但在 ios5 中创建字典时遇到问题,当输入空值时,它不会插入该键或此后在字典中。这是我的代码示例
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);
在 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;
}
ios5中的相同日志结果:
name: GamerLegend,address:haripad,city:(null), state:1, country:1, postal:(null) ,phone:(null), age:(null), gender:(null)
MyDetails:
{
address = India;
name = "GamerLegend";
}
我希望 ios5 的日志与 ios7 的日志相同。我做了一些搜索,我知道我可以事先比较该值以检查它是否为空,然后用空字符串添加到字典中。但这会很乏味。我想知道是否还有其他简单的解决方法。有没有更好的解决方案?
这是我目前的解决方案
-(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=@"";
}