Virtual
@propertyWrapper
public struct Virtual<V> : AttributeKeyPathStringConvertible, FieldAttributeProtocol
The containing type for computed property values. Because this value is not persisted to the backing store, any type is supported. Field.Virtual properties are not allowed to have initial values, including nil for optional types.
class Animal: CoreStoreObject {
@Field.Virtual(
"pluralName",
customGetter: { (object, field) in
return object.$species.value + "s"
}
)
var pluralName: String
@Field.Stored("species")
var species: String = ""
}
Important
Field properties are required to be used as @propertyWrappers. Any other declaration not using the @Field.Virtual(...) var syntax will be ignored.
-
Initializes the metadata for the property.
Field.Virtualproperties are not allowed to have initial values, includingnilfor optional types.class Person: CoreStoreObject { @Field.Virtual( "pluralName", customGetter: { (object, field) in return object.$species.value + "s" } ) var pluralName: String @Field.Stored("species") var species: String = "" }Declaration
Swift
public init( _ keyPath: KeyPathString, customGetter: @escaping (_ object: ObjectProxy<O>, _ field: ObjectProxy<O>.FieldProxy<V>) -> V, customSetter: ((_ object: ObjectProxy<O>, _ field: ObjectProxy<O>.FieldProxy<V>, _ newValue: V) -> Void)? = nil, affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = [])Parameters
keyPaththe permanent attribute name for this property.
customGetteruse this closure as an “override” for the default property getter. The closure receives a
ObjectProxy<O>, which acts as a type-safe proxy for the receiver. When accessing the property value fromObjectProxy<O>, make sure to usefield.primitiveValueinstead offield.value, which would unintentionally execute the same closure again recursively. Do not make assumptions on the thread/queue that the closure is executed on; accessors may be called fromNSErrorlogs for example.customSetteruse this closure as an “override” for the default property setter. The closure receives a
ObjectProxy<O>, which acts as a fast, type-safe KVC interface forCoreStoreObject. The reason aCoreStoreObjectinstance is not passed directly is because the Core Data runtime is not aware ofCoreStoreObjectproperties’ static typing, and so loading those info everytime KVO invokes this accessor method incurs a cumulative performance hit (especially in KVO-heavy operations such asListMonitorobserving.) When accessing the property value fromObjectProxy<O>, make sure to usefield.primitiveValueinstead offield.value, which would unintentionally execute the same closure again recursively.affectedByKeyPathsa set of key paths for properties whose values affect the value of the receiver. This is similar to
NSManagedObject.keyPathsForValuesAffectingValue(forKey:).
-
Declaration
Swift
public var cs_keyPathString: String { get }
-
Declaration
Swift
public typealias ObjectType = O -
Declaration
Swift
public typealias DestinationValueType = V
-
Declaration
Swift
public typealias ReturnValueType = DestinationValueType
-
Initializes the metadata for the property.
Field.Virtualproperties are not allowed to have initial values, includingnilfor optional types.class Person: CoreStoreObject { @Field.Virtual( "pluralName", customGetter: { (object, field) in return object.$species.value + "s" } ) var pluralName: String? @Field.Stored("species") var species: String = "" }Declaration
Swift
public init( _ keyPath: KeyPathString, customGetter: ((_ object: ObjectProxy<O>, _ field: ObjectProxy<O>.FieldProxy<V>) -> V)? = nil, customSetter: ((_ object: ObjectProxy<O>, _ field: ObjectProxy<O>.FieldProxy<V>, _ newValue: V) -> Void)? = nil, affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = [])Parameters
keyPaththe permanent attribute name for this property.
customGetteruse this closure as an “override” for the default property getter. The closure receives a
ObjectProxy<O>, which acts as a type-safe proxy for the receiver. When accessing the property value fromObjectProxy<O>, make sure to usefield.primitiveValueinstead offield.value, which would unintentionally execute the same closure again recursively. Do not make assumptions on the thread/queue that the closure is executed on; accessors may be called fromNSErrorlogs for example.customSetteruse this closure as an “override” for the default property setter. The closure receives a
ObjectProxy<O>, which acts as a fast, type-safe KVC interface forCoreStoreObject. The reason aCoreStoreObjectinstance is not passed directly is because the Core Data runtime is not aware ofCoreStoreObjectproperties’ static typing, and so loading those info everytime KVO invokes this accessor method incurs a cumulative performance hit (especially in KVO-heavy operations such asListMonitorobserving.) When accessing the property value fromObjectProxy<O>, make sure to usefield.primitiveValueinstead offield.value, which would unintentionally execute the same closure again recursively.affectedByKeyPathsa set of key paths for properties whose values affect the value of the receiver. This is similar to
NSManagedObject.keyPathsForValuesAffectingValue(forKey:).
View on GitHub
Virtual Structure Reference