Do NSDouble, NSFloat, or other types than NSInteger exist?

后端 未结 3 1440
北海茫月
北海茫月 2020-12-29 19:46

Over at In Cocoa do you prefer NSInteger or just regular int, and why?, there was mention of NSDouble and NSFloat, but I can\'t see a reference for

相关标签:
3条回答
  • 2020-12-29 19:51

    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.

    0 讨论(0)
  • 2020-12-29 20:06

    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.

    0 讨论(0)
  • 2020-12-29 20:15

    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.

    0 讨论(0)
提交回复
热议问题