ios - 使用 Firebase 异步字典下载创建对象数组 (Swift)

标签 ios arrays swift dictionary firebase

我是 Swift 的新手。我一直无法下载 Firebase 词典并将它们转换为对象数组。

下面的语法我做错了什么?在过去的两天里,我一直在试图解决这个问题,但没有成功。下面给了我一个索引超出范围的错误。这是因为 Firebase Dictionary 尚未完成下载还是我的 for in 循环语法有缺陷?也许两者都有?谢谢。

 // Array of Location Objects
 var locationsArray:[Location] = [Location]()

 var ref = Firebase(url: "<MYFIREBASEURL>")
 var dictionaryOfRecommendations:[NSDictionary] = [NSDictionary]()
 var currentlyConstructingLocation:Location = Location()

 func getLocationData() {

    let titleRef = self.ref.childByAppendingPath("events")
    titleRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        var tempDict = [NSDictionary]()

        for item in snapshot.children {

            let child = item as! FDataSnapshot
            let dict = child.value as! NSDictionary
            tempDict.append(dict)
        }

        self.dictionaryOfRecommendations = tempDict

    })

    // Parse data from Firebase

    // Loop through each dictionary and assign values to location object
    var index:Int
    for index in 0...dictionaryOfRecommendations.count {

        // Current Json dictionary
        let jsonDictionary:NSDictionary = self.dictionaryOfRecommendations[index]

        self.currentlyConstructingLocation.title = jsonDictionary["title"] as! String!
        self.currentlyConstructingLocation.locationsLatitudeArray = jsonDictionary["latitude"] as! Double
        self.currentlyConstructingLocation.locationsLongitudeArray = jsonDictionary["longitude"] as! Double

        // Append to Locations Array and start new Location
        self.locationsArray.append(currentlyConstructingLocation)
        self.currentlyConstructingLocation = Location()

    }

    // Notify the MainViewController that the Locations are ready.
    ...
}

最佳答案

以下是根据 Jay 的有用指导针对上述问题更新的正确代码:

//下载事件位置数据的模型。

//Firebase reference
var ref = Firebase(url: "<MYFIREBASEURL")

var locationsArray:[Location] = [Location]()
var dictionaryOfRecommendations:[NSDictionary] = [NSDictionary]()
var currentlyConstructingLocation:Location = Location()

 func getLocationData() {

    let titleRef = self.ref.childByAppendingPath("events")
    titleRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        var tempDict = [NSDictionary]()

        for item in snapshot.children {

            let child = item as! FDataSnapshot
            let dict = child.value as! NSDictionary
            tempDict.append(dict)

        }

        self.dictionaryOfRecommendations = tempDict
        self.ParseFirebaseData()

    })
}

func ParseFirebaseData() {
    // Parse data from Firebase

    // Loop through each dictionary and assign values to location object
    var index:Int
    for index in 0...dictionaryOfRecommendations.count - 1 {

        // Current Json dictionary
        let jsonDictionary:NSDictionary = self.dictionaryOfRecommendations[index]

        self.currentlyConstructingLocation.title = jsonDictionary["title"] as! String!
        self.currentlyConstructingLocation.locationsLatitudeArray = jsonDictionary["latitude"] as! Double
        self.currentlyConstructingLocation.locationsLongitudeArray = jsonDictionary["longitude"] as! Double

        // Append to Locations Array and start new Location
        self.locationsArray.append(currentlyConstructingLocation)
        self.currentlyConstructingLocation = Location()

    }
}

关于ios - 使用 Firebase 异步字典下载创建对象数组 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218247/

相关文章:

ios - 无法在 Swift 中观察 UIPageControl 的 currentPage 变化

ios - 在 UITableViewCell 突出显示时更改 accessoryView

ios - 如何在 iOS swift 中使用 firestore 获取用户的帖子数

ios - 等待多个 block 完成

javascript - 循环遍历数组并分配新数组的键

java - 为什么在 Weighted QuickUnion 中联合和查找操作期间的数组访问次数据说是 lg(N) 的顺序?

ios - 使用页眉和页脚创建自定义 Collection View

python - 仅计算 numpy 数组的 1 列中的非零数

swift 2 : How to parse a time signature from MIDIMetaEvent?

ios - 将图像转换为 NSData 以保存在核心数据中