MutableSet
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, MutableSet
s 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
.
-
Creates a
MutableSet
that holds Realm model objects of typeElement
.
-
Returns an
Array
containing the results of invokingvalueForKey(_:)
usingkey
on each of the collection’s objects.
-
Warning
Ordering is not guaranteed on a MutableSet. Subscripting is implement convenience should not be relied on.
-
Returns a Boolean value indicating whether the Set contains the given object.
-
Returns a Boolean value that indicates whether this set is a subset of the given set.
-
Returns a Boolean value that indicates whether this set intersects with another given set.
-
Inserts an object to the set if not already present.
Warning
This method may only be called during a write transaction.
-
Inserts the given sequence of objects into the set if not already present.
Warning
This method may only be called during a write transaction. -
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.
-
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. -
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.
-
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.
-
Inserts the elements of the given sequence into the set.
Warning
This method may only be called during a write transaction.
-
Returns a human-readable description of the objects contained in the MutableSet.
-
MutableSetElementMapper
transforms the actualMutableSet
ofObjects
orMutableSet
ofEmbeddedObjects
in toProjectedCollection
.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 viaprojectTo
Note: This is not the actual set data type therefore projected elements can contain duplicates.