1

I tried to use the updateData(fields: [AnyHashable : Any]) function to update an array in a document. It was working fine after a while I got a really strange error which I was unable to resolve, here's the error message:

global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'

----------------------------------------

CompileDylibError: Failed to build Data.swift

Compiling failed: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'

Data.swift:115:17: error: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
                __designTimeString("#64647.[4].[6].[3].modifier[2].arg[0].value.[0].key.[0].value", fallback: "reviews"): FieldValue.arrayUnion([id])
                ^
SwiftUI.__designTimeString:1:13: note: where 'T' = 'AnyHashable'
public func __designTimeString<T>(_ key: String, fallback: T) -> T where T : ExpressibleByStringLiteral

I have been looking around but couldn't really find much details on this kind of errors, please help :(

here's the part of my code that caused this error:

db.collection("restaurants").document(restaurantId).updateData([
     "reviews": FieldValue.arrayUnion([id])
     ]) { err in
        if let err = err {
            print("Error updating document: \(err)")
        } else {
            print("Document successfully updated")
        }
    }

2 Answers 2

1

I believe your error is being produced on the FieldValue.arrayUnion([id]) part. As shown in the Firestore documentation for adding data with Swift, the field has to be quoted, so you can replace:

"reviews": FieldValue.arrayUnion([id])

for

"reviews": FieldValue.arrayUnion(["id"])
Sign up to request clarification or add additional context in comments.

Comments

0

When using updateData, the keys need to be Strings.

Have you considered using Codable? Import FirebaseFirestoreSwift, and then save data using db.collection("restaurants").addDocument(from: review). More details here: SwiftUI: Mapping Firestore Documents using Swift Codable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.