List
public final class List<Element> : RLMSwiftCollectionBase, RealmCollectionImpl where Element : RealmCollectionValue
extension List: ObservableObject, RealmSubscribable
extension List: MutableCollection
extension List: Decodable where Element: Decodable
extension List: Encodable where Element: Encodable
List
is the container type in Realm used to define to-many relationships.
Like Swift’s Array
, List
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, List
s are reference types, and are only immutable if the Realm that manages them
is opened as read-only.
Lists can be filtered and sorted with the same predicates as Results<Element>
.
-
Creates a
List
that holds Realm model objects of typeElement
.Declaration
Swift
public override init()
-
Returns the object at the given index (get), or replaces the object at the given index (set).
Warning
You can only set an object during a write transaction.
Declaration
Swift
public subscript(position: Int) -> Element { get set }
Parameters
index
The index of the object to retrieve or replace.
-
Returns an
Array
containing the results of invokingvalueForKey(_:)
usingkey
on each of the collection’s objects.Declaration
Swift
@nonobjc public func value(forKey key: String) -> [AnyObject]
-
Returns an
Array
containing the results of invokingvalueForKeyPath(_:)
usingkeyPath
on each of the collection’s objects.Declaration
Swift
@nonobjc public func value(forKeyPath keyPath: String) -> [AnyObject]
Parameters
keyPath
The key path to the property whose values are desired.
-
Appends the given object to the end of the list.
If the object is managed by a different Realm than the receiver, a copy is made and added to the Realm managing the receiver.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func append(_ object: Element)
Parameters
object
An object.
-
Appends the objects in the given sequence to the end of the list.
Warning
This method may only be called during a write transaction.Declaration
Swift
public func append<S>(objectsIn objects: S) where Element == S.Element, S : Sequence
-
Inserts an object at the given index.
Warning
This method may only be called during a write transaction.
Warning
This method will throw an exception if called with an invalid index.
Declaration
Swift
public func insert(_ object: Element, at index: Int)
Parameters
object
An object.
index
The index at which to insert the object.
-
Removes an object at the given index. The object is not removed from the Realm that manages it.
Warning
This method may only be called during a write transaction.
Warning
This method will throw an exception if called with an invalid index.
Declaration
Swift
public func remove(at index: Int)
Parameters
index
The index at which to remove the object.
-
Removes all objects from the list. 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()
-
Replaces an object at the given index with a new object.
Warning
This method may only be called during a write transaction.
Warning
This method will throw an exception if called with an invalid index.
Declaration
Swift
public func replace(index: Int, object: Element)
Parameters
index
The index of the object to be replaced.
object
An object.
-
Moves the object at the given source index to the given destination index.
Warning
This method may only be called during a write transaction.
Warning
This method will throw an exception if called with invalid indices.
Declaration
Swift
public func move(from: Int, to: Int)
Parameters
from
The index of the object to be moved.
to
index to which the object at
from
should be moved. -
Exchanges the objects in the list at given indices.
Warning
This method may only be called during a write transaction.
Warning
This method will throw an exception if called with invalid indices.
Declaration
Swift
public func swapAt(_ index1: Int, _ index2: Int)
Parameters
index1
The index of the object which should replace the object at index
index2
.index2
The index of the object which should replace the object at index
index1
. -
Returns a human-readable description of the objects contained in the List.
Declaration
Swift
public override var description: String { get }
-
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<List> { get }
-
Replace the given
subRange
of elements withnewElements
.Declaration
Swift
public func replaceSubrange<C: Collection, R>(_ subrange: R, with newElements: C) where C.Iterator.Element == Element, R: RangeExpression, List<Element>.Index == R.Bound
Parameters
subrange
The range of elements to be replaced.
newElements
The new elements to be inserted into the List.
-
Declaration
Swift
public typealias SubSequence = Slice<List>
-
Returns the objects at the given range (get), or replaces the objects at the given range with new objects (set).
Warning
Objects may only be set during a write transaction.
Declaration
Swift
public subscript(bounds: Range<Int>) -> SubSequence { get set }
Parameters
index
The index of the object to retrieve or replace.
-
Removes the specified number of objects from the beginning of the list. 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 removeFirst(_ number: Int = 1)
-
Removes the specified number of objects from the end of the list. 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 removeLast(_ number: Int = 1)
-
Inserts the items in the given collection into the list at the given position.
Warning
This method may only be called during a write transaction.Declaration
Swift
public func insert<C>(contentsOf newElements: C, at i: Int) where Element == C.Element, C : Collection
-
Removes objects from the list at the given range.
Warning
This method may only be called during a write transaction.Declaration
Swift
public func removeSubrange<R>(_ boundsExpression: R) where R : RangeExpression, R.Bound == Int
-
Declaration
Swift
public convenience init(from decoder: Decoder) throws
-
projectTo
will map the originalList
ofObjects
orList
ofEmbeddedObjects
in toProjectedCollection
.For example:
class Person: Object { @Persisted var dogs: List<Dog> } class PersonProjection: Projection<Person> { @Projected(\Person.dogs.projectTo.name) var dogNames: ProjectedCollection<String> }
In this code the
Person
‘s dogs list will be prijected to the list of dogs names viaprojectTo
Declaration
Swift
public var projectTo: CollectionElementMapper<Element> { get }