How to convert an NSString to an unsigned int in Cocoa?

风格不统一 提交于 2019-12-03 14:00:27

The first version truncates and the second should be fine as long as your number actually fits into an unsigned int - see e.g. "Data Type Size and Alignment".
You should however create the NSNumber using +numberWithUnsignedInt.

If you know that the encoding is suitable, you could also simply go with the C-libraries:

unsigned n;
sscanf([str UTF8String], "%u", &n);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!