Appending a string to a UITextField
How can I append a string by adding '.' onto a UITextField? I have tried this: [textfield.text stringByAppendingString:@"."];
but I am getting strange beh开发者_Go百科avior - it converts the last character into a .
instead of adding on. (I also use this code to add 0-9 chars and it works perfectly.)
Any other methods of appending the .
and/or an explanations for this?
You need to do textfield.text = [textfield.text stringByAppendingString:@"."];
since the stringByAppendingString:
returns a new string, it doesn't modify the existing one (as strings are immutable by default).
Try this:
textfield.text = [NSString stringWithFormat:@"%@.", textfield.text];
Swift
CommentTextField.text = CommentTextField.text?.stringByAppendingString(string)
精彩评论