NSMutableArray vs NSArray which is better

后端 未结 6 1690
再見小時候
再見小時候 2021-02-05 18:11

This is a bit of a silly question, but if I want to add an object to an array I can do it with both NSMutableArray and NSArray, which should I use?

6条回答
  •  佛祖请我去吃肉
    2021-02-05 18:42

    NSMutableArray is not threadsafe, while NSArray is. This could be a huge problem if you're multithreading.
    NSMutableArray and NSArray both are build on CFArray, performance/complexity should be same. The access time for a value in the array is guaranteed to be at worst O(lg N) for any implementation, current and future, but will often be O(1) (constant time). Linear search operations similarly have a worst case complexity of O(N*lg N), though typically the bounds will be tighter, and so on. Insertion or deletion operations will typically be linear in the number of values in the array, but may be O(N*lg N) clearly in the worst case in some implementations.

提交回复
热议问题