DocumentID
@propertyWrapper
public struct DocumentID<Value: DocumentIDWrappable & Codable>:
StructureCodingUncodedUnkeyedextension DocumentID: Codableextension DocumentID: Equatable where Value: Equatableextension DocumentID: Hashable where Value: HashableA property wrapper type that marks a DocumentReference? or String? field to
be populated with a document identifier when it is read.
Apply the @DocumentID annotation to a DocumentReference? or String?
property in a Codable object to have it populated with the document
identifier when it is read and decoded from Firestore.
Important
The name of the property annotated with @DocumentID must not
match the name of any fields in the Firestore document being read or else
an error will be thrown. For example, if the Codable object has a
property named firstName annotated with @DocumentID, and the Firestore
document contains a field named firstName, an error will be thrown when
attempting to decode the document.
Example Read:
struct Player: Codable { @DocumentID var playerID: String? var health: Int64 }
let p = try! await Firestore.firestore() .collection(“players”) .document(“player-1”) .getDocument(as: Player.self) print(“(p.playerID!) Health: (p.health)”)
// Prints: “Player: player-1, Health: 95”
- Important: Trying to encode/decode this type using encoders/decoders other than
Firestore.Encoder throws an error.
- Important: When writing a Codable object containing an `@DocumentID` annotated field,
its value is ignored. This allows you to read a document from one path and
write it into another without adjusting the value here.
-
Undocumented
Declaration
Swift
public init(wrappedValue value: Value?) -
Undocumented
Declaration
Swift
public var wrappedValue: Value? { get set } -
A
Codableobject containing an@DocumentIDannotated field should only be decoded withFirestore.Decoder; this initializer throws if an unsupported decoder is used.Throws
FirestoreDecodingErrorDeclaration
Swift
public init(from decoder: Decoder) throwsParameters
decoderA decoder.
-
A
Codableobject containing an@DocumentIDannotated field can only be encoded withFirestore.Encoder; this initializer always throws.Throws
FirestoreEncodingErrorDeclaration
Swift
public func encode(to encoder: Encoder) throwsParameters
encoderAn invalid encoder.