How to take a NSString such as, "0+1+2", get the sum of those and create new string. Obj-C
Posted
by
Ace Legend
on Stack Overflow
See other posts from Stack Overflow
or by Ace Legend
Published on 2012-10-11T21:24:58Z
Indexed on
2012/10/11
21:37 UTC
Read the original article
Hit count: 178
Alright. My app has a text field and next to it are two buttons (Plus button and Equals button). When you press the "Plus" button, it takes the text inside of the textField and adds a "+" to it. Code below:
- (IBAction)plusButtonPressed:(id)sender {
NSString *plusString = @"+";
NSString *inputString = carbsField.text;
NSString *outputString = [NSString stringWithFormat:@"%@%@",inputString,plusString];
NSLog(@"%@",outputString);
[carbsField setText:outputString];
}
I will eventually make that more intelligent so that I can't put two pluses or whatever. Anyway, then I want the equal button to take whatever is in the textField, which should look something like: "23+54+2.2" and get the sum of those values. I believe I know how to take an Integer and make it a String, but I want to verify it:
int *value = 56;
NSString *string = @"%d",value;
Well, if anyone can show me how to do this, I would be very appreciative. Thanks.
EDIT
At the moment, I have not tried anything. This is because I do not know where to start. I have an idea of what to do, but I do not know how to execute it. I believe I need to get all characters before a "+" convert them into int
and then get the sum of some array of those values.
© Stack Overflow or respective owner