Returns the index, within a specified range, of an object compared with elements in the ordered set using a given NSComparator block.
SDKs
- iOS 5.0+
- macOS 10.7+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
func index(of object: Any, inSortedRange range: NSRange, options opts: NSBinary Searching Options = [], usingComparator cmp: (Any, Any) -> Comparison Result) -> Int
Parameters
object
An object for which to search in the ordered set.
If this value is
nil
, throws aninvalid
.Argument Exception range
The range within the array to search for
object
.If r exceeds the bounds of the ordered set (if the location plus length of the range is greater than the count of the ordered set), throws an
range
.Exception opts
Options for the search. For possible values, see
NSBinary
.Searching Options cmp
A comparator block used to compare the object obj with elements in the ordered set.
If this value is
NULL
, throws aninvalid
.Argument Exception
Return Value
If the insertion
option is not specified:
If the
object
is found and neitherfirst
norEqual last
is specified, returns the matching object's index.Equal If the
first
orEqual last
option is also specified, returns the index of equal objects.Equal If the object is not found, returns
NSNot
.Found
If the insertion
option is specified, returns the index at which you should insert obj
in order to maintain a sorted array:
If the
object
is found and neitherfirst
norEqual last
is specified, returns the matching object’s index.Equal If the
first
orEqual last
option is also specified, returns the index of the equal objects.Equal If the object is not found, returns the index of the least greater object, or the index at the end of the array if the object is larger than all other elements.
Discussion
The elements in the ordered set must have already been sorted using the comparator cmp
. If the ordered set is not sorted, the result is undefined.