ToMany

public final class ToMany<S, T>: ExpressibleByNilLiteral
where S: Store.InspectableEntity, T: Store.InspectableEntity

Declaration of a to-many relationship to objects of a certain type.

Initialize with nil in your type declarations. The code generator will set different values.

Example:

class Customer: Entity { var id: Id = 0

   /// Annotation with Source's property name is required; Order is "Source", Customer is "Target"
   // sourcery: backlink = "customer"
   var orders: ToMany<Order, Customer> = nil
   // ...

}

class Order: Entity { var id: Id = 0 var customer: ToOne = nil // … }

Removing relations

Since this type only supports backlinks at the moment, you cannot modify relations from this end. You have to set the relation from Source to Target to nil instead:

// You cannot set `aCustomer.orders = nil`, so:
let order = Array(aCustomer.orders)
orders.forEach { order in
    order.customer = nil
}
store.box(for: Order.self).put(orders)

Note

Not ready for storing to-many relations. Used only to get access to backlinks from ToOne relations. Use the // sourcery:backlink="propertyName" annotation to tell the code generator which property of ToMany.Source should be used to determine the backlinks.
  • The type that is having the relation to its many Target objects.

    Note

    Since we only support backlinks at the moment, this is the type of object that you will get when evaluating the backlink.

    Declaration

    Swift

    public typealias Source = S
  • The type of the objects that this relation points to.

    Note

    Since we only support backlinks at the moment, this is the type that you want to see the backlinks for.

    Declaration

    Swift

    public typealias Target = T
  • Initialize an empty backlink relation.

    Use this during object creation. The actual ToMany initialization with resolvable backlinks happens in ToMany.backlink(sourceBox:sourceProperty:targetId:) which is called by the code generator.

    Declaration

    Swift

    public init(nilLiteral: ())
  • Undocumented

    Declaration

    Swift

    public static func backlink(sourceBox: Box<Source>,
                                sourceProperty: Property<Source, Id<Target>>,
                                targetId: Id<Target>) -> ToMany<Source, Target>
  • Declaration

    Swift

    public typealias Index = Int
  • The position of the first element in a nonempty collection.

    Declaration

    Swift

    public var startIndex: Index { get }
  • The collections past the end position – that is, the position one greater than the last valid subscript argument.

    Declaration

    Swift

    public var endIndex: Index { get }
  • Returns the position immediately after the given index.

    Declaration

    Swift

    public func index(after i: Index) -> Index

    Parameters

    i

    A valid index of the collection. i must be less than endIndex.

    Return Value

    The index immediately after i.

  • Returns the position immediately before the given index.

    Declaration

    Swift

    public func index(before i: Index) -> Index

    Parameters

    i

    A valid index of the collection. i must be greater than startIndex.

    Return Value

    The index immediately before i.

  • Declaration

    Swift

    public subscript(position: Index) -> Source { get }
  • A textual representation of this instance.

    Declaration

    Swift

    public var description: String { get }
  • A textual representation of this instance, suitable for debugging.

    Declaration

    Swift

    public var debugDescription: String { get }