在 Cocoa 中,你更喜欢 NSInteger 还是 int,为什么? [英] In Cocoa do you prefer NSInteger or int, and why?
问题描述
NSInteger
/NSUInteger
是 Cocoa 定义的常规内置类型的替代品.
NSInteger
/NSUInteger
are Cocoa-defined replacements for the regular built-in types.
与内置函数相比,使用 NS* 类型有什么好处吗?你更喜欢哪个?为什么?NSInteger
和 int
在 32 位/64 位平台上的宽度是否相同?
Is there any benefit to using the NS* types over the built-ins? Which do you prefer and why? Are NSInteger
and int
the same width on 32-bit / 64-bit platforms?
推荐答案
我的理解是 NSInteger 等.是相应 C 类型的体系结构安全版本.基本上,它们的大小因架构而异,但例如 NSInteger 可以保证保存当前架构的任何有效指针.
The way I understand it is that NSInteger et al. are architecture safe versions of the corresponding C types. Basically their size vary depending on the architecture, but NSInteger, for example, is guaranteed to hold any valid pointer for the current architecture.
Apple 建议您在 OS X 10.5 及更高版本上使用它们,Apple 的 API:s 会使用它们,因此养成使用它们的习惯绝对是个好主意.它们需要更多的输入,但除此之外,似乎没有任何理由不使用它们.
Apple recommends that you use these to work with OS X 10.5 and onwards, and Apple's API:s will use them, so it's definitely a good idea to get into the habit of using them. They require a little more typing, but apart from that it doesn't seem to be any reason not to use them.
这篇关于在 Cocoa 中,你更喜欢 NSInteger 还是 int,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!