Search code examples
objective-cnsstringintnslog

Objective-C Calculating string value


This is my main:

int x=0;
NSString *new=[[NSString alloc]initWithString:@"9+4"];
x=[new intValue];
NSLog(@"hi %i",x);

This results in 9.. .since giving the intValue of a string will read only numbers and stops when the character is not a digit.

So how can i print the result of my string and get a 13 instead??


Solution

  • Change this line

    NSString *new=[[NSString alloc]initWithString:@"9+4"];
    

    to

    NSString *new=[NSString stringWithFormat:@"%f",9+4];