Attribute(_:originalName:hashModifier:)
Specifies the custom behavior that SwiftData applies to the annotated property when managing the owning class.
Declaration
@attached(peer) macro Attribute(_ options: Schema.Attribute.Option..., originalName: String? = nil, hashModifier: String? = nil)Parameters
- options:
A list of options to apply to the attached property to customize its behavior. For possible values, see Option.
- originalName:
The previous name of the attribute, if it’s different to the one in the current schema version. The default value is
nil. - hashModifier:
A unique hash value that represents the most recent version of the attached property. The default value is
nil.
Mentioned in
Overview
The framework’s default behavior for managing a model class’s stored properties is suitable for most use cases. However, if you need to alter the persistence behavior of a particular property, annotate it with the @Attribute macro. For example, you may want to avoid conflicts in your model data by specifying that an attribute’s value is unique across all instances of that model.
@Model
class RemoteImage {
@Attribute(.unique) var sourceURL: URL
var data: Data
init(sourceURL: URL, data: Data = Data()) {
self.sourceURL = sourceURL
self.data = data
}
}