Because of cocoa design patterns, the name of the instance method count in NSMutableArray is ambiguous; it could either return a saved variable count, or it could count the items in the array each time it’s called, and return that (cocoa dictates that a method that simply returns the value of a variable foo be foo, not getFoo). Does Objective C’s NSMutableArray actually count the items every time its count method is called, or does it return the value of a pre-calculated variable? If this were Java, I’d look at the source code, but I can’t find the source code for NSMutableArray. I looked at the API, and all it said was this:
Returns the number of objects currently in the array.
Since there was no context, I can’t tell if that means it does any calculations inside it or not.
NSArraydeclares a private ivar_usedthat appears to hold the count of the array. Usingobject_getInstanceVariable()you can verify that in the case of the concrete subclass__NSArrayM, this variable is incremented and decremented as objects are added and removed.I used the following program, a Mac OS X Foundation command-line tool, to verify this:
This program produced the following output: