On 31 May 2011, at 10:00, Alejandro Rodríguez wrote: > Hey Julius, > > The reason for using NSUInteger on such a high level framework as Cocoa may > not seem relevant but for the sake of completeness let me go down a road less > explored by many of the other answers. I think using NSUInteger vs NSInteger > is the result of a leaky abstraction. It's more than likely that it has to do > with the underlying implementation of NSArray which is surely based on C > arrays. > > If we go back to simpler times the use of Unsigned datatypes becomes really > obvious. For example… C was introduced in 1969 when only 8 bit processors > existed (16-bit processors were introduced in 1976) which means that using a > unsigned index could let you work with an array of 256 (0-255) elements vs > 128 (0-127). That alone justifies the decision completely. > > Let's explore the low level details a little more. Specifying the index of an > Array is really specifying an offset from a given pointer, if for example you > have a C Array in the stack then specifying a negative index will modify > previous data in your code which could lead to data loss (or even infinite > loops on some nicely engineered examples). However because of the nature of > the stack modifying some pointer further down (unsigned) has a less ill > effects (other than the well known buffer overflow security holes). When > designing APIs the designer takes the possibility of error into account and > it's usually preferred to expose errors that will cause the app to crash and > burn in flames instead of subtly corrupting data or getting into some weird > state that only crashes once every 6 months. For example: > > arr[(NSInteger)(0 - 2)] will corrupt data and it's likely that it won't crash. > arr[(NSUInteger)(0 - 2)] will probably always crash. > > Finally there is the point of scope. Objective-C started being designed and > used before 32-bit personal computers gained any traction so using unsigned > values made a huge difference. This is much much earlier that Cocoa but > NSArray is not part of the Cocoa framework but part of the Foundation > Framework which has a much broader spectrum. NSArray is toll-free bridged > with CFArray which being part of CoreFoundation is made to work > cross-platform and thus should try to be as generic as possible. It may be > hard to understand but in cases like these using the unsigned version is more > flexible because it is less wasteful. Using the type that best represents the > data is important here because using a signed integer to represent something > that can only nonnegative would in fact be wasting half of the space. > > Designing API is very tricky because many things once introduced are set in > stone (changing the interface would break existing applications) and many > things, including the programmer's bad memory, are taken into account. > > Hope this helps. > Regards, > > - Alej > Thanks. Exactly what I was looking for. Julius
http://juliuspaintings.co.uk _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com