# Optional

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

The containing type for optional transformable properties. Any type that conforms to `NSCoding & NSCopying` 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

`Transformable.Optional` properties are required to be stored properties. Computed properties will be ignored, including `lazy` and `weak` properties.

- `init(_:initial:isTransient:allowsExternalBinaryDataStorage:versionHashModifier:renamingIdentifier:customGetter:customSetter:affectedByKeyPaths:)`

  Initializes the metadata for the property.

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

      private static func getColor(_ partialObject: PartialObject<Animal>) -> UIColor? {
          if let cachedColor = partialObject.primitiveValue(for: { $0.color }) {
              return cachedColor
          }
          let color: UIColor?
          switch partialObject.value(for: { $0.species }) {

          case "Swift": color = UIColor.orange
          case "Bulbasaur": color = UIColor.green
          default: return nil
          }
          partialObject.setPrimitiveValue(color, for: { $0.color })
          return color
      }

  #### Declaration

  Swift

      public init(
          _ keyPath: KeyPathString,
          initial: @autoclosure @escaping () -> V? = nil,
          isTransient: Bool = false,
          allowsExternalBinaryDataStorage: 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 the `ImportableAttributeType`‘s empty value 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.
  `allowsExternalBinaryDataStorage` | `true` if the attribute allows external binary storage, otherwise `false`.
  `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 as an “override” for the default property getter. The closure receives a `PartialObject<O>`, which acts as a fast, type-safe KVC interface for `CoreStoreObject`. The reason a `CoreStoreObject` instance is not passed directly is because the Core Data runtime is not aware of `CoreStoreObject` properties’ static typing, and so loading those info everytime KVO invokes this accessor method incurs a cumulative performance hit (especially in KVO-heavy operations such as `ListMonitor` observing.) When accessing the property value from `PartialObject<O>`, make sure to use `PartialObject<O>.primitiveValue(for:)` instead of `PartialObject<O>.value(for:)`, which would unintentionally execute the same closure again recursively.

  `customSetter`

  use this closure as an “override” for the default property setter. The closure receives a `PartialObject<O>`, which acts as a fast, type-safe KVC interface for `CoreStoreObject`. The reason a `CoreStoreObject` instance is not passed directly is because the Core Data runtime is not aware of `CoreStoreObject` properties’ static typing, and so loading those info everytime KVO invokes this accessor method incurs a cumulative performance hit (especially in KVO-heavy operations such as `ListMonitor` observing.) When accessing the property value from `PartialObject<O>`, make sure to use `PartialObject<O>.setPrimitiveValue(_:for:)` instead of `PartialObject<O>.setValue(_:for:)`, which would unintentionally execute the same closure again recursively.

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

- `value`

  The attribute value

  #### Declaration

  Swift

      public var value: ReturnValueType { get set }

### AnyKeyPathStringConvertible

- `cs_keyPathString`

  #### Declaration

  Swift

      public var cs_keyPathString: String { get }

### KeyPathStringConvertible

- `ObjectType`

  #### Declaration

  Swift

      public typealias ObjectType = O

- `DestinationValueType`

  #### Declaration

  Swift

      public typealias DestinationValueType = V

### AttributeKeyPathStringConvertible

- `ReturnValueType`

  #### Declaration

  Swift

      public typealias ReturnValueType = DestinationValueType?

### TransformableContainer.Optional

- `observe(options:changeHandler:)`

  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, CoreStoreObjectTransformableDiff<V>) -> Void) -> CoreStoreObjectKeyValueObservation

  #### Parameters

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

### Operations

- `.=(_:_:)`

  Assigns an optional transformable value to the property. The operation

      animal.color .= UIColor.red

  is equivalent to

      animal.color.value = UIColor.red

  #### Declaration

  Swift

      public static func .= (property: TransformableContainer<O>.Optional<V>, newValue: V?)

- `.=(_:_:)`

  Assigns an optional transformable value from another property. The operation

      animal.color .= anotherAnimal.color

  is equivalent to

      animal.color.value = anotherAnimal.color.value

  #### Declaration

  Swift

      public static func .= <O2>(property: TransformableContainer<O>.Optional<V>, property2: TransformableContainer<O2>.Optional<V>) where O2 : CoreStoreObject

- `.=(_:_:)`

  Assigns a transformable value from another property. The operation

      animal.color .= anotherAnimal.color

  is equivalent to

      animal.color.value = anotherAnimal.color.value

  #### Declaration

  Swift

      public static func .= <O2>(property: TransformableContainer<O>.Optional<V>, property2: TransformableContainer<O2>.Required<V>) where O2 : CoreStoreObject