According to the NSMutableArray documentation:
removeLastObjectraises anNSRangeExceptionif there are no objects in the array.
For some reason, I seem to be able to call this method on an empty array, and no exception is thrown.
Here’s a test case:
- (void)testNSMutableArray
{
NSMutableArray* arr = [[NSMutableArray alloc] init];
STAssertTrue([arr count] == 0, @"Array count should be 0");
STAssertThrows([arr removeLastObject], @"Should throw NSRangeException");
}
This test case fails on the last line for me with the message:
[arr removeLastObject] raised (null). Should throw NSRangeException
Am I confused here? Is the documentation wrong?
Looking at the assembly, it appears this behaviour changed in Lion. Here’s a portion of the implementation of
[__NSArrayM removeLastObject](which is the actual implementation you’re calling):This calls
CFExecutableLinkedOnOrAfterwith a value of 7; if that returns 1 (which it does on my machine), you get the behaviour you’re seeing. If it returns 0, you get an exception.CFExecutableLinkedOnOrAfteris an undocumented function, but some snooping around suggests that it returns if the executable is linked after a particular version of Mac OS X. Some more snooping suggests that the value of 7 corresponds to 10.7.So, if you’re running Lion or later, you won’t get an exception. Sounds like a doc bug to me!