pol pal
pol pal

Reputation: 39

NSMutable string issue

I am implementing a simple calculator.

Each time the user clicks digits buttons, the sender.tag is appended to a nsmutable string called displayString with is then displayed on screen.

The mutable string is initiated with size 40.

Strangely, after entering 10 digits , for example 1111111111 , the string behaves wrongly.

If you continue to click on the digit button '1', the string doesn't append '1' anymore, but another value, like, 2 or 6 ... and suddenly a number like 25469632154 appears!

Checked with the Debugger - the problem does not come from the display but from the string itself with is not correctly appended .

Could it be the sender.tag is not passed correctly ?

What could go wrong here ?

[displayString appendString: [NSString stringWithFormat: @"%i", [sender tag]]];  
self.lblDisplay.text = displayString;

Upvotes: 0

Views: 76

Answers (2)

James
James

Reputation: 2282

Why are you using NSMutableString? Why not just do this:

self.lblDisplay.text = [self.lblDisplay.text stringByAppendingFormat:@"%d", [sender tag]];

Anyway, how are you setting the tags? Please post that code. My guess is that something is going wrong there, not in passing the tag or appending the value.

Upvotes: 1

Sergey Kuryanov
Sergey Kuryanov

Reputation: 6124

Welcome to SO! You can try to log all your tags to determine where problem is.

NSLog(@"%d", [sender tag])

Tag property is NSInteger type, so you can use %d modifier instead of %i. Also you can use appendFormat method:

[displayString appendFormat:@"%d",[sender tag]];
self.lblDisplay.text = displayString;

Upvotes: 1

Related Questions