It seems to me that NSArray's practice of throwing out of bounds errors is very inconsistent with the behaviors of the other Foundation storage classes.
As far as I know, every other storage class returns nil when it's asked to access something that it doesn't have.
Does anyone know the reasoning behind this decision? It seems like it forces me as the programmer to add in checks everywhere like
If NSArrays simply behaved the same as every foundation storage class, I could achieve the same thing in a single line of code, with just
Will it silently fail? Perhaps, but I personally really like the ability to send messages to nil. It makes for extremely simple code where when an object doesn't exist nothing should happen, but if it does exist something should be done with it.
As far as I know, every other storage class returns nil when it's asked to access something that it doesn't have.
Does anyone know the reasoning behind this decision? It seems like it forces me as the programmer to add in checks everywhere like
Code:
NSObject *object;
if (array.count) {
object = array[0];
}
If NSArrays simply behaved the same as every foundation storage class, I could achieve the same thing in a single line of code, with just
Code:
NSObject *object = array[0];
Will it silently fail? Perhaps, but I personally really like the ability to send messages to nil. It makes for extremely simple code where when an object doesn't exist nothing should happen, but if it does exist something should be done with it.