字符串到双重故障排除
如何在 xcode 3.2 中正确地将 NSString 转换为 double ?这是我的代码(缩写),但是当我运行它时,消息显示(null)。我做错了什么?非常感谢!
@synthesize class1sem1;
-(IBAction)buttonPressed
{
NSString *myString = class1sem1.text;
double myDouble = [myString doubleValue];
NSString *message = [[NSString alloc] initWithFormat:
@"%@", myDouble];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Result" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
How do I correctly convert a NSString to a double in xcode 3.2? This is my code(abbreviated), but when I run it, the message says (null). What am I doing wrong? Thanks so much!
@synthesize class1sem1;
-(IBAction)buttonPressed
{
NSString *myString = class1sem1.text;
double myDouble = [myString doubleValue];
NSString *message = [[NSString alloc] initWithFormat:
@"%@", myDouble];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Result" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 %g 代表你的双精度..而不是 %@
Use %g for your double .. not %@
您尝试将双精度数显示为对象。请使用它:
f
表示“双精度浮点数”,而@
表示“Objective-C 对象”。double
是双精度浮点数,而不是 Objective-C 对象。You try to display a double as if it were an object. Use this instead:
f
means "double precision floating point number", while@
means "Objective-C object". Adouble
is a double precision floating point number, not an Objective-C object.