Reputation: 259
I have a piece of code:
NSString *favoriteName = nil;
if (self.favTextField.text != nil) {
favoriteName = self.favTextField.text;
} else
{
favoriteName = self.defaultName;
}
This is for when I press save name button, and the textfield text is nil it will be the default name. However, when I click on the textfield but don't type anything and save. It will save empty. How can I fix that, I want it will still save the default name if I click on the text field but don't type anything? Please help me out.
Btw: i have try self.favTextField.text isEqualToString:@"" it still does not work.
Upvotes: 1
Views: 151
Reputation: 2064
You could try testing for string length.
if ([self.favTextField.text length]==0) {
}
Upvotes: 0
Reputation: 29767
NSString *trimmedString = [self.favTextField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmedString.length > 0){
favoriteName = self.favTextField.text;
}
else{
favoriteName = self.defaultName;
}
Upvotes: 1