开发者

Appending a string to a UITextField

开发者 https://www.devze.com 2023-02-22 07:35 出处:网络
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

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消