Does NSMutableArray actually count the items every time its count method is called?

前端 未结 2 1419
囚心锁ツ
囚心锁ツ 2021-01-13 17:27

Because of cocoa design patterns, the name of the instance method count in NSMutableArray is ambiguous; it could either return a saved variable

2条回答
  •  难免孤独
    2021-01-13 17:58

    As you have correctly noted, there is no guarantee that it will behave one way or the other.

    In practice, though, -[NSArray count] is a constant-time operation. You can confirm this yourself by creating a small array and a large array and benchmarking the time it takes to get their counts. It would be pretty silly to have an O(n) count method on the core array classes like this.

提交回复
热议问题