Do NSDouble, NSFloat, or other types than NSInteger exist?
Over at In Cocoa do you prefer NSInteger or just regular int, and why?, there was mention of NSDouble
and NSF开发者_JS百科loat
, but I can't see a reference for those in any documentation. If NSInteger
's purpose is for architectural safety, what about other types such as double
or float
?
NSInteger
exists because the int
type varies in size between 32-bit and 64-bit systems. float
and double
don't vary in size the same way, so there's no need to have wrapper types for them.
There is no NSFloat
but I know the Core Graphics API eventually changed from float
to CGFloat
so that it could use a double
on some architectures.
It is best to use the exact types that API headers declare. This makes type changes automatic if you ever recompile your code for a different target.
It's also about conventions.
A typedef to an int is incompatible to int int itself.
Example: pid_t is of type int, but passing an int would create a warning.
Why? Because you want to be sure that if you cross API boundaries everyone knows what the code expects.
There are float and double types, i.e NSTimeInterval. It's not really about the underlying type, but the convention to adhere to.
If you declare a local int as a loop counter and you do not plan to pass it to a well-defined API, it's fine to call an int an int.
精彩评论