Objective-C计算字符串值 [英] Objective-C Calculating string value

查看:155
本文介绍了Objective-C计算字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主要内容:

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

这导致9 .. .since给出一个字符串的intValue将只读取数字并停止时这个字符不是数字。

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

那么如何打印我的字符串的结果并获得13而不是?

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

推荐答案

更改此行

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

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

这篇关于Objective-C计算字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆