简体   繁体   中英

“sortedArrayHint” method of NSArray class, what is the purpose of this method and how is it used?

Question is the same as the title. ("sortedArrayHint" method of NSArray class, what is the purpose of this method and how is it used)

I read documentation but the explanation is not clear.

Please explain the purpose of this method and its usage.

The idea is simple. Assume you have a large array that should always be sorted. Changing or inserting even one element means you have to resort the array. Sorting is costly.

The method -[NSArray sortedArrayHint] can be called on an already sorted array in order to get private internal data that can be used to speed up a sort of the same array given that only a small change has been made.

Usage is simple:

  1. Get and store the hint from the original sorted array using -[NSArray sortedArrayHint] .
  2. After a small change; resort using -[NSArray sortedArrayUsingFunction:context:hint:] with the stored hint.
  3. After a large change; resort using -[NSArray sortedArrayUsingFunction:context:] , and get a new hint.

What is a small, or large, change is something you must measure with Instruments.

I never use this myself, since I have found it more effective to use my own categories on NSArray and NSMutabelArray for sorted inserts, that uses a binary search, on sorted array. My code is available as open source here: https://github.com/Jayway/CWFoundation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM