user648244
user648244

Reputation: 1278

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 behavior - 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?

Upvotes: 1

Views: 6530

Answers (3)

manoj kumar
manoj kumar

Reputation: 37

Swift

CommentTextField.text = CommentTextField.text?.stringByAppendingString(string)

Upvotes: 1

DarkDust
DarkDust

Reputation: 92414

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).

Upvotes: 8

Nick Weaver
Nick Weaver

Reputation: 47241

Try this:

textfield.text = [NSString stringWithFormat:@"%@.", textfield.text];

Upvotes: 2

Related Questions