iOS Developer Library

Developer

Foundation Framework Reference NSFastEnumeration Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSFastEnumeration

The fast enumeration protocol NSFastEnumeration must be adopted and implemented by objects used in conjunction with the for language construct used in conjunction with Cocoa objects.

The abstract class NSEnumerator provides a convenience implementation that uses nextObject to return items one at a time. For more details, see Fast Enumeration.

Inheritance


Not Applicable

Conforms To


Not Applicable

Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.
  • Returns by reference a C array of objects over which the sender should iterate, and as the return value the number of objects in the array. (required)

    Declaration

    Swift

    func countByEnumeratingWithState(_ state: UnsafeMutablePointer<NSFastEnumerationState>, objects stackbuf: AutoreleasingUnsafeMutablePointer<AnyObject?>, count len: Int) -> Int

    Objective-C

    - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id [])stackbuf count:(NSUInteger)len

    Parameters

    state

    Context information that is used in the enumeration to, in addition to other possibilities, ensure that the collection has not been mutated.

    stackbuf

    A C array of objects over which the sender is to iterate.

    len

    The maximum number of objects to return in stackbuf.

    Return Value

    The number of objects returned in stackbuf. Returns 0 when the iteration is finished.

    Discussion

    The state structure is assumed to be of stack local memory, so you can recast the passed in state structure to one more suitable for your iteration.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

Data Types

  • This defines the structure used as contextual information in the NSFastEnumeration protocol.

    Declaration

    Swift

    struct NSFastEnumerationState { var state: UInt var itemsPtr: AutoreleasingUnsafeMutablePointer<AnyObject?> var mutationsPtr: UnsafeMutablePointer<UInt> var extra: (UInt, UInt, UInt, UInt, UInt) }

    Objective-C

    typedef struct { unsigned long state; id *itemsPtr; unsigned long *mutationsPtr; unsigned long extra[5]; } NSFastEnumerationState;

    Discussion

    For more information, see countByEnumeratingWithState:objects:count:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.