将 NSInteger 转换为 NSUInteger?

发布于 2024-11-28 04:43:23 字数 71 浏览 2 评论 0原文

我正在尝试将 NSInteger 转换为 NSUInteger,我用 google 搜索了它,但没有找到真正的答案。我该怎么做?

I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吾性傲以野 2024-12-05 04:43:23

NSIntegerNSUInteger 只是原始整数类型的 typedef:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

因此,您不需要在它们之间“转换”。一个简单的转换就足够了。喜欢:

NSInteger myInt = 0;
NSUInteger unsignedInt = (NSUInteger)myInt;

NSInteger and NSUInteger are just typedefs for primitive integer types:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

As such, you don't need to "convert" between them. A simple cast should be sufficient. Like:

NSInteger myInt = 0;
NSUInteger unsignedInt = (NSUInteger)myInt;
筱果果 2024-12-05 04:43:23

由于这可能对其他遇到此问题的人有用,因此这里有一个小表格,向您展示了转换的实际效果。这些值是直接从调试器中以十六进制值形式获取的。进行相应的选择,正如您所看到的,投射确实会产生效果。对于 32 位,删除底部 ffffffff,对于 16 位,删除底部 ffffffffffff。另请注意,-1 始终为 0xffffffffffffffff。

NSInteger si = NSIntegerMax;    // si   NSInteger   0x7fffffffffffffff
si = -1;                        // si   NSInteger   0xffffffffffffffff
si = (NSInteger)-1;             // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)-1;            // si   NSInteger   0xffffffffffffffff
si = NSUIntegerMax;             // si   NSInteger   0xffffffffffffffff
si = (NSInteger)NSIntegerMax;   // si   NSInteger   0x7fffffffffffffff
si = (NSUInteger)NSIntegerMax;  // si   NSInteger   0x7fffffffffffffff
si = (NSInteger)NSUIntegerMax;  // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)NSUIntegerMax; // si   NSInteger   0xffffffffffffffff

NSUInteger ui = NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = -1;                        // ui   NSUInteger  0xffffffffffffffff
ui = (NSInteger)-1;             // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)-1;            // ui   NSUInteger  0xffffffffffffffff
ui = NSIntegerMax;              // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSIntegerMax;   // ui   NSUInteger  0x7fffffffffffffff
ui = (NSUInteger)NSIntegerMax;  // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)NSUIntegerMax; // ui   NSUInteger  0xffffffffffffffff

Since this might be useful for other coming across this issue here is a little table showing you the actual effect of casting. These values were taken straight from the debugger as hex values. Choose accordingly, as you can see casting does cause effects. For 32-bit, lop off the bottom ffffffff and for 16-bit lop off bottom ffffffffffff. Also note, -1 is always 0xffffffffffffffff.

NSInteger si = NSIntegerMax;    // si   NSInteger   0x7fffffffffffffff
si = -1;                        // si   NSInteger   0xffffffffffffffff
si = (NSInteger)-1;             // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)-1;            // si   NSInteger   0xffffffffffffffff
si = NSUIntegerMax;             // si   NSInteger   0xffffffffffffffff
si = (NSInteger)NSIntegerMax;   // si   NSInteger   0x7fffffffffffffff
si = (NSUInteger)NSIntegerMax;  // si   NSInteger   0x7fffffffffffffff
si = (NSInteger)NSUIntegerMax;  // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)NSUIntegerMax; // si   NSInteger   0xffffffffffffffff

NSUInteger ui = NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = -1;                        // ui   NSUInteger  0xffffffffffffffff
ui = (NSInteger)-1;             // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)-1;            // ui   NSUInteger  0xffffffffffffffff
ui = NSIntegerMax;              // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSIntegerMax;   // ui   NSUInteger  0x7fffffffffffffff
ui = (NSUInteger)NSIntegerMax;  // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)NSUIntegerMax; // ui   NSUInteger  0xffffffffffffffff
装纯掩盖桑 2024-12-05 04:43:23

如果您确定您的 NSInteger 大于或等于 0,则只需将其转换为 NSUInteger 即可。如果您的 NSInteger 表示负数,则不应转换为 NSUInteger,因为这可能会导致不一致。

NSInteger signedInteger = -30;
NSUInteger unsignedInteger = (NSUInteger)signedInteger;

结果为

integer results

if (unsignedInteger == signedInteger) {
    // is TRUE
}
if (signedInteger == 4294967266) {
    // is FALSE
}
if (signedInteger == -30) {
    // is TRUE
}
if (unsignedInteger == 4294967266) {
    // is TRUE
}
if (unsignedInteger == -30) {
    // is TRUE
}

If you are certain that your NSInteger is greater than or equal to zero, you simply cast it as NSUInteger. You should not be casting to NSUInteger if your NSInteger represents a negative number, as it may lead to inconsistency.

NSInteger signedInteger = -30;
NSUInteger unsignedInteger = (NSUInteger)signedInteger;

results in

integer results

if (unsignedInteger == signedInteger) {
    // is TRUE
}
if (signedInteger == 4294967266) {
    // is FALSE
}
if (signedInteger == -30) {
    // is TRUE
}
if (unsignedInteger == 4294967266) {
    // is TRUE
}
if (unsignedInteger == -30) {
    // is TRUE
}
小清晰的声音 2024-12-05 04:43:23

如果您想要将整数转换为无符号整数,那么您要么知道该整数永远不会为负数,要么您可能希望将所有负值转换为绝对值(即无符号值) 。转换为绝对值非常简单:

NSInteger mySignedInteger = -100;
NSUInteger myUnsignedInteger = fabs(mySignedInteger);

fabs() 是一个 C 函数,可为任何浮点数返回绝对值。这里,myUnsignedInteger 的值为 100。

将显示此类函数的可能用例,您可以在其中以不同的格式指示负值。例如,在会计中,负数显示在括号中:

NSString * numberForDisplay;
if (mySignedInteger < 0) {
    numberForDisplay = [NSString stringWithFormat:@"(%lu)", myUnsignedInteger];
} else {
    numberForDisplay = [NSString stringWithFormat:@"%lu", myUnsignedInteger];
}

请注意,从技术角度来看,您可以简单地将 NSInteger 分配给 NSUInteger - 这甚至不需要强制转换 - 但当 NSInteger 为负数时,NSUInteger 将返回非常大的正数,这显然是非常不受欢迎的副作用:

NSUInteger myUnsignedInteger = mySignedInteger;

If you are wanting to convert an integer to an unsigned integer, you are either in a situation where you know that the integer will never be negative, or you may be wanting all negative values to be converted to absolute values (i.e., unsigned values). Doing this conversion to an absolute value is straightforward:

NSInteger mySignedInteger = -100;
NSUInteger myUnsignedInteger = fabs(mySignedInteger);

fabs() is a C function that returns an absolute value for any float. Here, myUnsignedInteger would have a value of 100.

Possible use cases for such a function would be display where you will indicate the negative values in a different format. For instance, in accounting negative numbers are displayed in parentheses:

NSString * numberForDisplay;
if (mySignedInteger < 0) {
    numberForDisplay = [NSString stringWithFormat:@"(%lu)", myUnsignedInteger];
} else {
    numberForDisplay = [NSString stringWithFormat:@"%lu", myUnsignedInteger];
}

Note that from a technical point of view, you could simply assign an NSInteger to an NSUInteger—this does not even require a cast—but when the NSInteger is negative the NSUInteger will return a very large positive number, which is clearly a highly undesirable side effect:

NSUInteger myUnsignedInteger = mySignedInteger;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文