目标C - 类型铸造从NSString到Int [英] Objective C - Type Casting From NSString to Int

查看:73
本文介绍了目标C - 类型铸造从NSString到Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Objective C代码,其中我将一个 NSString 转换为 int



I have this bit of Objective C code, where I am casting a NSString to an int:

NSString *a=@"123abc";
NSInteger b=(int) a;
NSLog(@"b: %d",b);

并且 NSLog 生成此输出: / p>

And the NSLog produces this output:

b: 18396

任何人都可以向我解释为什么会发生这种情况?

Can anyone explain to me why this is happening?

我在展示类型下将字符串转换为整数将从字符串中获取数值。

I was under the impression type casting a string to an integer would get the numerical value from the string.

推荐答案

你有整数值指向 NSString 对象。要将字符串解析为整数,您应该这样做:

You've got integer value of pointer to NSString object there. To parse string to integer you should do:

NSString *a = @"123abc";
NSInteger b = [a integerValue];

这篇关于目标C - 类型铸造从NSString到Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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