使用随机值Swift从JSON加载特定数据

时间:2016-08-26 00:46:15

标签: swift xcode firebase firebase-realtime-database

我正在使用firebase,我正在尝试从 Firebase数据库中检索数据,但是当我调用“postDict”时,它正常运行。

["-KPZAOg58kUdqwYMzlDJ": 
{
description = jpeg;
listingImageURL = "https://firebasestorage.googleapis.com/v0/b/hype-central.appspot.com/o/lidsafstings%2FC73FdfB8A0-968df7-44DB-B96B-A10DC1E3A2B7.png?alt=media&token=b6e296f2-8854-4b76-9bfc-470dfd69d11f2f";
location = jpeg;
price = 54;
title = jpeg;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
}, 
"-KPZ0yLhcfLvP9YWNjbC": {
description = "Description ofvaroe";
listingImageURL = "https://firebadfasestorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2F0ACdasfCD645-2E2C-4936-B3DF-37EC55AA0157.png?alt=media&token=b321dasff42f-75b5-4eb3-b129-b0a902cc5926";
location = Vancouver;
price = 34;
title = eclipse;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
},
"-KPoCsNS63JZdbTJIdaP": {
description = Brandon;
listingImageURL = "https://firebadasfsestorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2FOptional(%22pRuwvL7WyzQpY0G22ZLYCmTTemB3%22)%2F3EACE28E-D28A-464A-B6D0-FB502E9dafsB4775.jpeg?alt=media&token=1d45bc7b-7273-4d33-8ccd-755f3102a23c";
location = BRANDON;
price = BRANDON;
title = BRANDON;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
}, 
"-KP_Y7ug7hwrHtW6VNqV": {
description = "Description ofvaroe";
listingImageURL = "https://firebaseddasfstorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2FC5daf7991BA-D7E0-4C8A-96E7-0DB17EEBABD7.jpeg?alt=media&token=efd03b4d-6964-4685-9b48-5ed36a4ceb59";
location = "";
price = "";
title = "";
userDisplayName = "Brandon Mayhew";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
}]

如果我调用“postDict [” - KPZAOg58kUdqwYMzlDJ“] [”title“]”它将打印标题,这正是我想要的但是如何获得这个JSON格式的所有标题的数组(不仅仅是标题为“-KPZAOg58kUdqwYMzlDJ”)。

如何获得所有标题?

1 个答案:

答案 0 :(得分:1)

使用: -

rootRef.observeEventType(.Value, withBlock: {(postDictionary) in

  if let postDict = postDictionary.value as? [String:AnyObject]{

        for each in postDict as [String:AnyObject]{

              let autoID = each.0

                rootRef.child(autoID).observeEventType(.Value, withBlock: {(specificPost) in

                  //RETRIEVE YOUR DATA


                })

           }

        }

   })

PS: - 我已经使用了rootRef,因为你没有在JSON结构中指定父节点:)