Nat! bio photo

Nat!

Senior Mull.

Twitter RSS

Github

NSInteger ? Why not just int ?

Being old enough to have witnessed the migration from 16 bit int to bit int, I assumed that eventually int would become 64 bit. With this NSInteger thingy, it's been a big surprise. Because if int had just naturally grown to 64 bit, we wouldn't need this NSInteger at all.

I think somewhere in the C-Standard it even says, that int can be expected to be the "natural" fit for the CPU.

That the Cocoa APIs were designed using int and unsigned int for years instead of using size_t or some such like the rest of the world, to me only ever made sense, if indeed int was basically used as the migration type. That's one of the main features of C to define types not strictly as having a certain size, expressly intended for upwards migration... to belabor that point.

If I were to change all by ints to NSInteger using that tops script, I'd have the identical runtime problems as if the compiler had changed the meaning from 32 bit to 64 bit.

So where's the benefit ?


2 Comments

A photo of Chris Hanson

From: Chris Hanson

This is required because Mac OS X is an LP64 platform, not an ILP64 platform. At the Cocoa level, however, NSInteger and NSUInteger allow Cocoa to "act like" an ILP64 platform, which is closer to what many developers working at that level will expect.

C/C++ code expecting int to be 32 bits (of which there is a LOT, otherwise LP64 wouldn't even exist) will continue to work, while Objective-C code that can deal with NSInteger being the CPU's natural word width will also continue to work. And since you'll have to do some porting anyway to get a 64-bit app up and running due to deprecations and runtime differences (whereas that's not necessarily the case for plain C/C++ code) it's just one more bit to deal with.

A photo of

From:

I can imagine that not having any 32bit type can be problematic. Which goes to show that I have been living for years under a wrong assumption...

Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: