Returns the index of an object in the array that passes a test in a given block for a given set of enumeration options.
SDKs
- iOS 4.0+
- macOS 10.6+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
func indexOfObject(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int
Parameters
opts
A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).
predicate
The block to apply to elements in the array.
The block takes three arguments:
- obj
The element in the array.
- idx
The index of the element in the array.
- stop
A reference to a Boolean value. The block can set the value to
true
to stop further enumeration of the array. If a block stops further enumeration, that block continues to run until it’s finished. When theNSEnumeration
enumeration option is specified, enumeration stops after all of the currently running blocks finish. TheConcurrent stop
argument is an out-only argument. You should only ever set this Boolean totrue
within the block.
The block returns a Boolean value that indicates whether
obj
passed the test.
Return Value
The index whose corresponding value in the array passes the test specified by predicate
and opts
. If the opts
bit mask specifies reverse order, then the last item that matches is returned. Otherwise, the index of the first matching object is returned. If no objects in the array pass the test, returns NSNot
.
Discussion
By default, the enumeration starts with the first object and continues serially through the array to the last object. You can specify NSEnumeration
and/or NSEnumeration
as enumeration options to modify this behavior.
Important
If the block parameter is nil
this method will raise an exception.