如何在目标C中循环遍历Firebase childByAutoId

时间:2018-09-24 14:15:46

标签: ios objective-c firebase firebase-realtime-database nsmutablearray

我需要遍历childByAutoId以在数组中添加帖子。那么如何访问价格,标题,文本字符串以将该数据加载到表中?

这是我的基本结构:enter image description here

我从帖子中读取数据

  [[_ref child:@"posts"]
 observeEventType:FIRDataEventTypeValue
 withBlock:^(FIRDataSnapshot *snapshot) {

     [self.postData removeAllObjects];

     // Loop over children
     NSEnumerator *children = [snapshot children];
     FIRDataSnapshot *child;
     while (child = [children nextObject]) {
         NSLog(@"child: %@", child);

         [self.postData addObject:child];
     }

     NSLog(@"postData: %@", self.postData);

     [self.postCollectionView reloadData];

 }];

这是我的日志:

2018-09-24 16:59:03.788023+0300 App[4904:1202383] child: Snap (-LNAfBgHiac83feC6Wm1) {
    price = 3747;
    text = Jfjfjf;
    title = Jfjfjf;
}
2018-09-24 16:59:03.788125+0300 App[4904:1202383] child: Snap (-LNAfY-W7OZMA4Jm7TQN) {
    price = 3764647;
    text = Kgkgkg;
    title = Blnckckc;
}
2018-09-24 16:59:03.788176+0300 App[4904:1202383] child: Snap (-LNArUfHbIV3y_hgACLr) {
    title = Kgjggkkgnnc;
}
2018-09-24 16:59:03.788312+0300 App[4904:1202383] postData: (
    "Snap (-LNAfBgHiac83feC6Wm1) {\n    price = 3747;\n    text = Jfjfjf;\n    title = Jfjfjf;\n}",
    "Snap (-LNAfY-W7OZMA4Jm7TQN) {\n    price = 3764647;\n    text = Kgkgkg;\n    title = Blnckckc;\n}",
    "Snap (-LNArUfHbIV3y_hgACLr) {\n    title = Kgjggkkgnnc;\n}"
)

1 个答案:

答案 0 :(得分:0)

就这么简单

[self.postData addObject:child];

更改为

[self.postData addObject:child.value];