如何访问更深节点级别的数据?

发布于 2025-01-12 02:31:57 字数 3152 浏览 0 评论 0原文

我正在使用 Swift 开发一个应用程序。 这是我创建结构并将数据发送到数据库的地方:

func addRecord(userID: String,
   recordDate: String?, recordTime: String?,
   mealCategoryTitle: String?, mealCategoryImage: String?,
   breakfastMealTitle: String?, breakfastMealImage: String?,
   lunchDinMealTitle: String?, lunchDinMealImage: String?,
   snackMealTitle: String?, snackMealImage: String?,
   treatMealTitle: String?, treatMealImage: String?,
   drinkTitle: String?, drinkImage: String?,
   foodNote: String?,
   placeTitle: String?, placeImage: String?,
   moodTitle: String?, moodImage: String?,
   reactionTitle: String?, reactionImage: String?,
   moodNote: String?) {
        
        ref?.child("Records").childByAutoId().setValue([
            "userID": userID,
            "date": recordDate!,
            "time": recordTime!,
            "mealCategory" : [  "itemTitle": mealCategoryTitle, "itemImage": mealCategoryImage  ],
            "breakfastMeal" : [ "itemTitle": breakfastMealTitle, "itemImage": breakfastMealImage ],
            "lunchDinnerMeal" : [ "itemTitle": lunchDinMealTitle, "itemImage": lunchDinMealImage ],
            "snackMeal" : [ "itemTitle": snackMealTitle, "itemImage": snackMealImage ],
            "treatMeal" : [ "itemTitle": treatMealTitle, "itemImage": treatMealImage ],
            "drink" : [ "itemTitle": drinkTitle,"itemImage": drinkImage ],
            "foodNote": foodNote ?? "nil" ,
            "place" : [ "itemTitle": placeTitle, "itemImage": placeImage ],
            "mood" : [ "itemTitle": moodTitle, "itemImage": moodImage ],
            "reaction" : [ "itemTitle": reactionTitle, "itemImage": reactionImage ],
            "moodNote": moodNote ?? "nil" ,])
        
        //later use this key to set Status node
//        ref?.child("Records").child(key).child(moodNote).setValue.( /// set the rest of values)
//
    }

当我读取数据时,我只能访问第一级,但需要例如 MealCategoryTitle 和 Image。

func readRecords(reloadedTableView: UITableView ){
    ref?.child("Records").observe(.childAdded, with: { (snapshot) in
        if let rrecord = snapshot.value as? [String: Any] {
            let userID = rrecord["userID"] as? String ?? ""
            let date = rrecord["date"] as? String ?? ""
            let time = rrecord["time"] as? String ?? ""
            // problem
            let node: NSArray = snapshot.children.allObjects as NSArray
            let childNode = node.[0]
            let foodNote = rrecord["foodNote"] as? String ?? ""
            let moodNote = rrecord["moodNote"] as? String ?? ""
            let actualRecord = Record(userID: userID, date: date, time: time, mealCategory: ItemDetail(itemTitle: "problem", itemImage:nil), breakfastMeal: nil, lunchDinMeal: nil, snackMeal: nil, treatMeal: nil, drink: nil, foodNote: foodNote, place: nil, mood: nil, reaction: nil, moodNote: moodNote)
            self.records.append(actualRecord)
            print(actualRecord)
            print("This is snap:\(node)")
            print("This is child:\(childNode)")
            reloadedTableView.reloadData()

        }
    })
    { error in
      print(error.localizedDescription)
    }

I am developing an app with Swift.
this is where I am creating the structure and sending data to database:

func addRecord(userID: String,
   recordDate: String?, recordTime: String?,
   mealCategoryTitle: String?, mealCategoryImage: String?,
   breakfastMealTitle: String?, breakfastMealImage: String?,
   lunchDinMealTitle: String?, lunchDinMealImage: String?,
   snackMealTitle: String?, snackMealImage: String?,
   treatMealTitle: String?, treatMealImage: String?,
   drinkTitle: String?, drinkImage: String?,
   foodNote: String?,
   placeTitle: String?, placeImage: String?,
   moodTitle: String?, moodImage: String?,
   reactionTitle: String?, reactionImage: String?,
   moodNote: String?) {
        
        ref?.child("Records").childByAutoId().setValue([
            "userID": userID,
            "date": recordDate!,
            "time": recordTime!,
            "mealCategory" : [  "itemTitle": mealCategoryTitle, "itemImage": mealCategoryImage  ],
            "breakfastMeal" : [ "itemTitle": breakfastMealTitle, "itemImage": breakfastMealImage ],
            "lunchDinnerMeal" : [ "itemTitle": lunchDinMealTitle, "itemImage": lunchDinMealImage ],
            "snackMeal" : [ "itemTitle": snackMealTitle, "itemImage": snackMealImage ],
            "treatMeal" : [ "itemTitle": treatMealTitle, "itemImage": treatMealImage ],
            "drink" : [ "itemTitle": drinkTitle,"itemImage": drinkImage ],
            "foodNote": foodNote ?? "nil" ,
            "place" : [ "itemTitle": placeTitle, "itemImage": placeImage ],
            "mood" : [ "itemTitle": moodTitle, "itemImage": moodImage ],
            "reaction" : [ "itemTitle": reactionTitle, "itemImage": reactionImage ],
            "moodNote": moodNote ?? "nil" ,])
        
        //later use this key to set Status node
//        ref?.child("Records").child(key).child(moodNote).setValue.( /// set the rest of values)
//
    }

When I am reading the data, I am only capable to access the first level, but need the MealCategoryTitle and Image for example.

func readRecords(reloadedTableView: UITableView ){
    ref?.child("Records").observe(.childAdded, with: { (snapshot) in
        if let rrecord = snapshot.value as? [String: Any] {
            let userID = rrecord["userID"] as? String ?? ""
            let date = rrecord["date"] as? String ?? ""
            let time = rrecord["time"] as? String ?? ""
            // problem
            let node: NSArray = snapshot.children.allObjects as NSArray
            let childNode = node.[0]
            let foodNote = rrecord["foodNote"] as? String ?? ""
            let moodNote = rrecord["moodNote"] as? String ?? ""
            let actualRecord = Record(userID: userID, date: date, time: time, mealCategory: ItemDetail(itemTitle: "problem", itemImage:nil), breakfastMeal: nil, lunchDinMeal: nil, snackMeal: nil, treatMeal: nil, drink: nil, foodNote: foodNote, place: nil, mood: nil, reaction: nil, moodNote: moodNote)
            self.records.append(actualRecord)
            print(actualRecord)
            print("This is snap:\(node)")
            print("This is child:\(childNode)")
            reloadedTableView.reloadData()

        }
    })
    { error in
      print(error.localizedDescription)
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2025-01-19 02:31:57

如果您从快照中获取 mealCategory,则会为您提供一个字典,您可以从中访问该键下的属性。

例如,要获取 mealCategoryitemTitle,您可以这样做:

let mealCategoryTitle = rrecord["mealCategory"]["itemTitle"]

您不必强制转换该 rrecord["mealCategory"] 的类型 据我所知,但如果需要,它的类型将是rrecord["mealCategory"] as? Dictionaryrrecord["mealCategory"] 为? [字符串:任意]

If you get the mealCategory out of the snapshot, that'll give you a dictionary from which you can then access the properties under that key.

So for example to get the itemTitle of the mealCategory, you'd do:

let mealCategoryTitle = rrecord["mealCategory"]["itemTitle"]

You shouldn't have to cast the type of that rrecord["mealCategory"] as far as I can tell, but if that is needed its type would be rrecord["mealCategory"] as? Dictionary<String, AnyObject> or rrecord["mealCategory"] as? [String:Any].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文