One Logo Pool Ball
RealityFoundation · IOS API REFERENCE

Entity.ComponentSet

struct · iOS 13.0+ · SDK 27.1

@MainActor @preconcurrency struct ComponentSet

A collection of components that an entity stores. A `ComponentSet` represents all the components that an entity holds. Use this set to add, remove, and update components on an entity. This set can hold one component of each type. Access the `ComponentSet` of an ``Entity`` using its ``Entity/components`` property. `ComponentSet` conforms to <doc://com.apple.documentation/documentation/swift/sequence>, allowing you to iterate over it to access and use each component, as the example below shows: ```swift for component in entity.components { print(component) } ```

Members (15)

get · iOS 18.0+

startIndex

TypeScript
get startIndex(): Promise<Entity_ComponentSet_Index>
Swift declaration
@MainActor @preconcurrency var startIndex: Entity.ComponentSet.Index { get }

The position of the first element in a nonempty collection. If the collection is empty, `startIndex` is equal to `endIndex`.

method · iOS 18.0+

makeIterator

TypeScript
makeIterator(): Promise<Entity_ComponentSet_Iterator>
Swift declaration
@MainActor @preconcurrency func makeIterator() -> Entity.ComponentSet.Iterator

Returns an iterator over the elements of this sequence.

method · iOS 27.0+

set

TypeScript
setQualifiedComponentNameRepresentation(qualifiedComponentName: string, representation: SDKHandle): Promise<void>
Swift declaration
@MainActor @preconcurrency func set(qualifiedComponentName: String, representation: some Encodable) throws

Adds component data to an entity that is written to a Reality file but has no other effect at author time. Use this function from an authoring tool to serialize a component whose Swift type isn't available on the authoring platform. `representation` is encoded into any Reality file this entity is written to, and is decoded back into a ``Component`` of the type named by `qualifiedComponentName` when that file is loaded, so it must encode to the layout that component expects. For runtime use on a platform where the component's Swift type is available, set the component directly with `set(_:)` instead. ```swift // Serialize a custom component by its fully qualified name. struct MyCustomComponent: Component, Codable { var intensity: Float } try entity.components.set( qualifiedComponentName: "MyModule.MyCustomComponent", representation: MyCustomComponent(intensity: 0.5)) ``` - Parameters: - qualifiedComponentName: The component's fully qualified, module-qualified name. - representation: A value that encodes to the component's expected layout. - Throws: An error if `representation` can't be encoded, or if `qualifiedComponentName` uses a reserved prefix.

method · iOS 13.0+

set

TypeScript
set(components: Array<Implements<Component> | SDKHandle>): Promise<void>
Swift declaration
@MainActor @preconcurrency func set(_ components: [any Component])

Adds multiple components to the set, overriding any existing components of the same type. If the input array includes multiple components of the same type, the set adds the component with the highest index. This is because the set can only hold one component of each type. - Parameter components: An array of components to add.

get · iOS 13.0+

count

TypeScript
get count(): Promise<number>
Swift declaration
@MainActor @preconcurrency var count: Int { get }

The number of components in the collection.

method · iOS 18.0+

index

TypeScript
indexAfter(index: Entity_ComponentSet_Index): Promise<Entity_ComponentSet_Index>
Swift declaration
@MainActor @preconcurrency func index(after index: Entity.ComponentSet.Index) -> Entity.ComponentSet.Index

Returns the position immediately after the given index. The successor of an index must be well defined. For an index `i` into a collection `c`, calling `c.index(after: i)` returns the same index every time. - Parameter i: A valid index of the collection. `i` must be less than `endIndex`. - Returns: The index value immediately after `i`.

method · iOS 26.0+

index

TypeScript
indexBefore(index: Entity_ComponentSet_Index): Promise<Entity_ComponentSet_Index>
Swift declaration
@MainActor @preconcurrency func index(before index: Entity.ComponentSet.Index) -> Entity.ComponentSet.Index

Returns the position immediately before the given index. - Parameter i: A valid index of the collection. `i` must be greater than `startIndex`. - Returns: The index value immediately before `i`.

get · iOS 13.0+

entity

TypeScript
get entity(): Promise<Entity>
Swift declaration
@MainActor @preconcurrency var entity: Entity { get }
get · iOS 18.0+

indices

TypeScript
get indices(): Promise<Entity_ComponentSet_Indices>
Swift declaration
@MainActor @preconcurrency var indices: Entity.ComponentSet.Indices { get }

The indices that are valid for subscripting the collection, in ascending order. A collection's `indices` property can hold a strong reference to the collection itself, causing the collection to be nonuniquely referenced. If you mutate the collection while iterating over its indices, a strong reference can result in an unexpected copy of the collection. To avoid the unexpected copy, use the `index(after:)` method starting with `startIndex` to produce indices instead. var c = MyFancyCollection([10, 20, 30, 40, 50]) var i = c.startIndex while i != c.endIndex { c[i] /= 5 i = c.index(after: i) } // c == MyFancyCollection([2, 4, 6, 8, 10])

get · iOS 18.0+

isEmpty

TypeScript
get isEmpty(): Promise<boolean>
Swift declaration
@MainActor @preconcurrency var isEmpty: Bool { get }

A Boolean value indicating whether the collection is empty. When you need to check whether your collection is empty, use the `isEmpty` property instead of checking that the `count` property is equal to zero. For collections that don't conform to `RandomAccessCollection`, accessing the `count` property iterates through the elements of the collection. let horseName = "Silver" if horseName.isEmpty { print("My horse has no name.") } else { print("Hi ho, \(horseName)!") } // Prints "Hi ho, Silver!" - Complexity: O(1)

method · iOS 18.0+

distance

TypeScript
distance(start: Entity_ComponentSet_Index, end: Entity_ComponentSet_Index): Promise<number>
Swift declaration
@MainActor @preconcurrency func distance(from start: Entity.ComponentSet.Index, to end: Entity.ComponentSet.Index) -> Int

Returns the distance between two indices. Unless the collection conforms to the `BidirectionalCollection` protocol, `start` must be less than or equal to `end`. - Parameters: - start: A valid index of the collection. - end: Another valid index of the collection. If `end` is equal to `start`, the result is zero. - Returns: The distance between `start` and `end`. The result can be negative only if the collection conforms to the `BidirectionalCollection` protocol. - Complexity: O(1) if the collection conforms to `RandomAccessCollection`; otherwise, O(*k*), where *k* is the resulting distance.

get · iOS 18.0+

endIndex

TypeScript
get endIndex(): Promise<Entity_ComponentSet_Index>
Swift declaration
@MainActor @preconcurrency var endIndex: Entity.ComponentSet.Index { get }

The collection's "past the end" position---that is, the position one greater than the last valid subscript argument. When you need a range that includes the last element of a collection, use the half-open range operator (`..<`) with `endIndex`. The `..<` operator creates a range that doesn't include the upper bound, so it's always safe to use with `endIndex`. For example: let numbers = [10, 20, 30, 40, 50] if let index = numbers.firstIndex(of: 30) { print(numbers[index ..< numbers.endIndex]) } // Prints "[30, 40, 50]" If the collection is empty, `endIndex` is equal to `startIndex`.

method · iOS 18.0+

formIndex

TypeScript
formIndex(i: Entity_ComponentSet_Index): Promise<{ $ret: null; $out: { "i": Entity_ComponentSet_Index } }>
Swift declaration
@MainActor @preconcurrency func formIndex(after i: inout Entity.ComponentSet.Index)

Replaces the given index with its successor. - Parameter i: A valid index of the collection. `i` must be less than `endIndex`.

Omit the output parameters from the call. The result contains $ret and $out, with i inside $out.

method · iOS 13.0+

removeAll

TypeScript
removeAll(): Promise<void>
Swift declaration
@MainActor @preconcurrency func removeAll()

Removes all components from the collection.

subscript · iOS 18.0+

subscript

TypeScript
subscript(position: Entity_ComponentSet_Index): Promise<SDKHandle>
Swift declaration
@MainActor @preconcurrency subscript(position: Entity.ComponentSet.Index) -> any Component { get }

Accesses the element at the specified position. The following example accesses an element of an array through its subscript to print its value: var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"] print(streets[1]) // Prints "Bryant" You can subscript a collection with any valid index other than the collection's end index. The end index refers to the position one past the last element of a collection, so it doesn't correspond with an element. - Parameter position: The position of the element to access. `position` must be a valid index of the collection that is not equal to the `endIndex` property. - Complexity: O(1)

Skipped declarations

12 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

Entity.ComponentSet.subscript(_:_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(withoutAnimation:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.set(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

Entity.ComponentSet.subscript(componentType:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(_:_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(componentType:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.subscript(_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.set(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

Entity.ComponentSet.has(_:)

Passing a Swift type itself is not supported.

Entity.ComponentSet.remove(_:)

Passing a Swift type itself is not supported.

All RealityFoundation types →