ToOne

public final class ToOne<T>: ExpressibleByNilLiteral
where T: Store.InspectableEntity

Object property type to indicate a to-one relation to another object.

Initialize with nil in your type declarations.

  • You can set the value to nil to remove the relation. You can also set target or targetId to nil.
  • You can set target to an object to set the relation. Call Box.put(_:) to persist the changes.
  • You can set targetId to an object’s ID to set the relation. Call Box.put(_:) to persist the changes.

Example

class Person: Entity {
    var spouse: ToOne<Person> = nil
}

let personBox: Box<Person> = store.box(for: Person.self)
let amanda: Person = ...
let bob: Person = ...

amanda.spouse.target = bob
personBox.put(amanda)

Remove a relation

amanda.spouse.target = nil
// ... or ...
amanda.spouse.targetId = nil
// ... are equivalent to:
amanda.spouse = nil
// ... whis is a short version of:
amanda.spouse = ToOne<Person>(target: nil)
  • The type of object this relation will produce.

    Declaration

    Swift

    public typealias Target = T
  • Whether the relation was set to a target object.

    Declaration

    Swift

    public var hasValue: Bool { get }
  • Access to the relation’s target, if any. Set to nil to remove the relation.

    Alternatively, you can also overwrite the whole relation:

    order.customer.target = nil
    // ... is equivalent to:
    order.customer = nil
    // ... whis is a short version of:
    order.customer = ToOne<Customer>(target: nil)
    

    Note

    Call Box.put(_:) to persist changes.

    Declaration

    Swift

    public var target: Target? { get set }
  • Access to the ID of the relation’s target, if any. Set to nil to remove the relation.

    Alternatively, you can also overwrite the whole relation:

    order.customer.target = nil
    // ... is equivalent to:
    order.customer = nil
    // ... whis is a short version of:
    order.customer = ToOne<Customer>(target: nil)
    

    Note

    Call Box.put(_:) to persist changes.

    Declaration

    Swift

    public var targetId: Id<Target>? { get set }
  • Initialize an empty relation.

    Use this during entity creation, like:

    class Order { var customer: ToOne = nil // … }

    Declaration

    Swift

    public required init(nilLiteral: ())
  • Initialize a relation with a target set from the get-go.

    Declaration

    Swift

    public required init(_ entity: Target?)

    Parameters

    entity

    Target entity, or nil if the relation is empty.

  • 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 }