简体   繁体   中英

Rename an array title in Firebase Firestore | Swift

Is there a good way to rename an array's title in Swift? For example, I'd like to be able to change the array title "Cats" to "Dogs" (without it losing its values). I was updating the entire "Animals" dictionary, but surely there's a more efficient way to do this (update just the single sub-dictionary title).

Dictionary setup:

" Animals " (main dictionary)

-> Cats (sub-dictionary)

-> -> "Jimmy", "Lola", "Timmy" (cat names)

let db = Firestore.firestore()
        
        guard let userID = Auth.auth().currentUser?.uid else {
            print("The user is not signed into an account.")
            return
        }
        
        if userID.isEmpty != true {
            // Inside the "Animals" array, change the "Cats" dictionary title to "Dogs"
        }

There is no specific operation to rename a field, so you'll have to perform the operation by:

  1. Reading the document to get the current array contents.
  2. Writing the array with its new field name.
  3. Remove the array from its old field name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM