MutableSet

public final class MutableSet<Element> : RLMSwiftCollectionBase, RealmCollectionImpl where Element : RealmCollectionValue
extension MutableSet: ObservableObject, RealmSubscribable
extension MutableSet: Decodable where Element: Decodable
extension MutableSet: Encodable where Element: Encodable

MutableSet is the container type in Realm used to define to-many relationships with distinct values as objects.

Like Swift’s Set, MutableSet is a generic type that is parameterized on the type it stores. This can be either an Object subclass or one of the following types: Bool, Int, Int8, Int16, Int32, Int64, Float, Double, String, Data, Date, Decimal128, and ObjectId (and their optional versions)

Unlike Swift’s native collections, MutableSets are reference types, and are only immutable if the Realm that manages them is opened as read-only.

MutableSet’s can be filtered and sorted with the same predicates as Results<Element>.

Properties of MutableSet type defined on Object subclasses must be declared as let and cannot be dynamic.

Initializers

  • Creates a MutableSet that holds Realm model objects of type Element.

    Declaration

    Swift

    public override init()

KVC

  • Returns an Array containing the results of invoking valueForKey(_:) using key on each of the collection’s objects.

    Declaration

    Swift

    @nonobjc
    public func value(forKey key: String) -> [AnyObject]

Object Retrieval

  • Warning

    Ordering is not guaranteed on a MutableSet. Subscripting is implement convenience should not be relied on.

    Declaration

    Swift

    public subscript(position: Int) -> Element { get }

Filtering

  • Returns a Boolean value indicating whether the Set contains the given object.

    Declaration

    Swift

    public func contains(_ object: Element) -> Bool

    Parameters

    object

    The element to find in the MutableSet.

  • Returns a Boolean value that indicates whether this set is a subset of the given set.

    Declaration

    Swift

    public func isSubset(of possibleSuperset: MutableSet<Element>) -> Bool

    Parameters

    object

    Another MutableSet to compare.

  • Returns a Boolean value that indicates whether this set intersects with another given set.

    Declaration

    Swift

    public func intersects(_ otherSet: MutableSet<Element>) -> Bool

    Parameters

    object

    Another MutableSet to compare.

Mutation

  • Inserts an object to the set if not already present.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func insert(_ object: Element)

    Parameters

    object

    An object.

  • Inserts the given sequence of objects into the set if not already present.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func insert<S>(objectsIn objects: S) where Element == S.Element, S : Sequence
  • Removes an object in the set if present. The object is not removed from the Realm that manages it.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func remove(_ object: Element)

    Parameters

    object

    The object to remove.

  • Removes all objects from the set. The objects are not removed from the Realm that manages them.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func removeAll()
  • Mutates the set in place with the elements that are common to both this set and the given sequence.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func formIntersection(_ other: MutableSet<Element>)

    Parameters

    other

    Another set.

  • Mutates the set in place and removes the elements of the given set from this set.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func subtract(_ other: MutableSet<Element>)

    Parameters

    other

    Another set.

  • Inserts the elements of the given sequence into the set.

    Warning

    This method may only be called during a write transaction.

    Declaration

    Swift

    public func formUnion(_ other: MutableSet<Element>)

    Parameters

    other

    Another set.

  • Returns a human-readable description of the objects contained in the MutableSet.

    Declaration

    Swift

    public override var description: String { get }

MutableSet

  • A publisher that emits Void each time the collection changes.

    Despite the name, this actually emits after the collection has changed.

    Declaration

    Swift

    public var objectWillChange: RealmPublishers.WillChange<MutableSet> { get }

Available where Element: Decodable

Available where Element: ObjectBase & RealmCollectionValue

  • MutableSetElementMapper transforms the actual MutableSet of Objects or MutableSet of EmbeddedObjects in to ProjectedCollection.

    For example:

     class Person: Object {
         @Persisted var dogs: MutableSet<Dog>
     }
     class PersonProjection: Projection<Person> {
         @Projected(\Person.dogs.projectTo.name) var dogNames: ProjectedCollection<String>
     }
    

    In this code the Person‘s dogs set will be prijected to the projected set of dogs names via projectTo Note: This is not the actual set data type therefore projected elements can contain duplicates.

    Declaration

    Swift

    public var projectTo: CollectionElementMapper<Element> { get }