本文介绍了NSUInteger VS NSInteger的,INT VS无符号,和类似的案件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人有专业知识,讲解时使用NSUInteger,何时使用NSInteger的?

我看到了Cocoa方法返回NSInteger的,即使在情况下,返回值将始终是无符号的。

什么是根本原因?是NSInteger的或者int严格限制,如果我们要重新present负值?

从NSObjCRuntime.h:

 #如果__LP64__ || (TARGET_OS_EMBEDDED&安培;&安培;!TARGET_OS_IPHONE)|| TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
长期的typedef NSInteger的;
无符号的typedef长NSUInteger;
#其他
的typedef诠释NSInteger的;
的typedef unsigned int类型NSUInteger;
#万一


解决方案

您应该也与NSUInteger处理与NSInteger的时候要注意的整数转换规则:

例如下面的片段返回0(假),虽然你期望它来打印1(真):

  NSInteger的SI = -1;
NSUInteger UI = 1;
的printf(%d个\\ N,硅< UI);

究其原因是,[SI]变量被隐式转换为一个unsigned int!

请参阅解决这些问题。

Anyone have the expertise to explain when to use NSUInteger and when to use NSInteger?

I had seen Cocoa methods returning NSInteger even in cases where the returned value will always be unsigned.

What is the fundamental reason? Is NSInteger or int strictly limited to if we want to represent negative value?

From NSObjCRuntime.h:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
解决方案

You should also be aware of integer conversion rules when dealing with NSUInteger vs. NSInteger:

The following fragment for example returns 0 (false) although you'd expect it to print 1 (true):

NSInteger si = -1;
NSUInteger ui = 1;
printf("%d\n", si < ui);

The reason is that the [si] variable is being implicitly converted to an unsigned int!

See CERT's Secure Coding site for an in-depth discussion around these 'issues' and how to solve them.

这篇关于NSUInteger VS NSInteger的,INT VS无符号,和类似的案件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:40