devsite/tools/jazzy/templates

FirebaseFirestore Framework Reference

DocumentID

@propertyWrapper
public struct DocumentID<Value: DocumentIDWrappable & Codable>:
  StructureCodingUncodedUnkeyed
extension DocumentID: Codable
extension DocumentID: Equatable where Value: Equatable
extension DocumentID: Hashable where Value: Hashable

A 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 Codable object containing an @DocumentID annotated field should only be decoded with Firestore.Decoder; this initializer throws if an unsupported decoder is used.

    Declaration

    Swift

    public init(from decoder: Decoder) throws

    Parameters

    decoder

    A decoder.

  • A Codable object containing an @DocumentID annotated field can only be encoded with Firestore.Encoder; this initializer always throws.

    Declaration

    Swift

    public func encode(to encoder: Encoder) throws

    Parameters

    encoder

    An invalid encoder.