Optional

public final class Optional<V> : AttributeKeyPathStringConvertible, AttributeProtocol where V : ImportableAttributeType

The containing type for optional value properties. Any type that conforms to ImportableAttributeType are supported.

class Animal: CoreStoreObject {
    let species = Value.Required<String>("species", initial: "")
    let nickname = Value.Optional<String>("nickname")
    let color = Transformable.Optional<UIColor>("color")
}

Important

Value.Optional properties are required to be stored properties. Computed properties will be ignored, including lazy and weak properties.
  • Initializes the metadata for the property.

    class Person: CoreStoreObject {
        let title = Value.Optional<String>("title", initial: "Mr.")
        let name = Value.Optional<String>("name")
        let displayName = Value.Optional<String>(
            "displayName",
            isTransient: true,
            customGetter: Person.getName(_:)
        )
    
        private static func getName(_ partialObject: PartialObject<Person>) -> String? {
            if let cachedDisplayName = partialObject.primitiveValue(for: { $0.displayName }) {
               return cachedDisplayName
            }
            let title = partialObject.value(for: { $0.title })
            let name = partialObject.value(for: { $0.name })
            let displayName = "\(title) \(name)"
            partialObject.setPrimitiveValue(displayName, for: { $0.displayName })
            return displayName
        }
    }
    

    Declaration

    Swift

    public init(
        _ keyPath: KeyPathString,
        initial: @autoclosure @escaping () -> V? = nil,
        isTransient: Bool = false,
        versionHashModifier: @autoclosure @escaping () -> String? = nil,
        renamingIdentifier: @autoclosure @escaping () -> String? = nil,
        customGetter: ((_ partialObject: PartialObject<O>) -> V?)? = nil,
        customSetter: ((_ partialObject: PartialObject<O>, _ newValue: V?) -> Void)? = nil,
        affectedByKeyPaths: @autoclosure @escaping () -> Set<String> = [])

    Parameters

    keyPath

    the permanent attribute name for this property.

    initial

    the initial value for the property that is shared for all instances of this object. Note that this is evaluated during DataStack setup, not during object creation. Defaults to nil if not specified.

    isTransient

    true if the property is transient, otherwise false. Defaults to false if not specified. The transient flag specifies whether or not a property’s value is ignored when an object is saved to a persistent store. Transient properties are not saved to the persistent store, but are still managed for undo, redo, validation, and so on.

    versionHashModifier

    used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)

    renamingIdentifier

    used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property and a destination entity property that share the same identifier indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s name.

    customGetter

    use this closure to make final transformations to the property’s value before returning from the getter.

    self
    getValue

    the original getter for the property

    customSetter

    use this closure to make final transformations to the new value before assigning to the property.

    setValue

    the original setter for the property

    finalNewValue

    the transformed new value

    originalNewValue

    the original new value

    affectedByKeyPaths

    a set of key paths for properties whose values affect the value of the receiver. This is similar to NSManagedObject.keyPathsForValuesAffectingValue(forKey:).

  • The attribute value

    Declaration

    Swift

    public var value: ReturnValueType { get set }

AnyKeyPathStringConvertible

  • Declaration

    Swift

    public var cs_keyPathString: String { get }

KeyPathStringConvertible

AttributeKeyPathStringConvertible

ValueContainer.Optional

  • Observes changes in the receiver value. When the returned CoreStoreObjectKeyValueObservation is deinited or invalidated, it will stop observing.

    Declaration

    Swift

    public func observe(options: NSKeyValueObservingOptions = [], changeHandler: @escaping (O, CoreStoreObjectValueDiff<V>) -> Void) -> CoreStoreObjectKeyValueObservation

    Parameters

    options

    The flags indicating which values to include in the change dictionary.

  • Creates a Where clause by comparing if a property is equal to a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.nickname == "John" }))
    

    Declaration

    Swift

    public static func == (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by comparing if a property is not equal to a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.nickname != "John" }))
    

    Declaration

    Swift

    public static func != (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by comparing if a property is less than a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.age < 20 }))
    

    Declaration

    Swift

    public static func < (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by comparing if a property is greater than a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.age > 20 }))
    

    Declaration

    Swift

    public static func > (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by comparing if a property is less than or equal to a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.age <= 20 }))
    

    Declaration

    Swift

    public static func <= (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by comparing if a property is greater than or equal to a value

    let person = dataStack.fetchOne(From<Person>().where({ $0.age >= 20 }))
    

    Declaration

    Swift

    public static func >= (attribute: ValueContainer<O>.Optional<V>, value: V?) -> Where<O>
  • Creates a Where clause by checking if a sequence contains the value of a property

    let dog = dataStack.fetchOne(From<Dog>().where({ ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname }))
    

    Declaration

    Swift

    public static func ~= <S>(sequence: S, attribute: ValueContainer<O>.Optional<V>) -> Where<O> where V == S.Element, S : Sequence

Operations

  • Assigns an optional value to the property. The operation

    animal.nickname .= "Taylor"
    

    is equivalent to

    animal.nickname.value = "Taylor"
    

    Declaration

    Swift

    public static func .= (property: ValueContainer<O>.Optional<V>, newValue: V?)
  • Assigns an optional value from another property. The operation

    animal.nickname .= anotherAnimal.nickname
    

    is equivalent to

    animal.nickname.value = anotherAnimal.nickname.value
    

    Declaration

    Swift

    public static func .= <O2>(property: ValueContainer<O>.Optional<V>, property2: ValueContainer<O2>.Optional<V>) where O2 : CoreStoreObject
  • Assigns a value from another property. The operation

    animal.nickname .= anotherAnimal.species
    

    is equivalent to

    animal.nickname.value = anotherAnimal.species.value
    

    Declaration

    Swift

    public static func .= <O2>(property: ValueContainer<O>.Optional<V>, property2: ValueContainer<O2>.Required<V>) where O2 : CoreStoreObject
  • Compares equality between a property’s value and another value

    if animal.species .== "Swift" { ... }
    

    is equivalent to

    if animal.species.value == "Swift" { ... }
    

    Declaration

    Swift

    public static func .== (property: ValueContainer<O>.Optional<V>, value: V?) -> Bool
  • Compares equality between a property’s value and another property’s value

    if "Swift" .== animal.species { ... }
    

    is equivalent to

    if "Swift" == animal.species.value { ... }
    

    Declaration

    Swift

    public static func .== (value: V?, property: ValueContainer<O>.Optional<V>) -> Bool
  • Compares equality between a property’s value and another property’s value

    if animal.species .== anotherAnimal.species { ... }
    

    is equivalent to

    if animal.species.value == anotherAnimal.species.value { ... }
    

    Declaration

    Swift

    public static func .== (property: ValueContainer<O>.Optional<V>, property2: ValueContainer<O>.Optional<V>) -> Bool
  • Compares equality between a property’s value and another property’s value

    if animal.species .== anotherAnimal.species { ... }
    

    is equivalent to

    if animal.species.value == anotherAnimal.species.value { ... }
    

    Declaration

    Swift

    public static func .== (property: ValueContainer<O>.Optional<V>, property2: ValueContainer<O>.Required<V>) -> Bool