开发者社区> 问答> 正文

iOS如何使用Swift代码检索快照计数?

在我的应用程序中,有一个我要在其上显示图像的集合视图。这些图像是从Firebase数据库检索的。

Firebase数据库的结构如下

{ "CHILD1" : { "URL1" : "https://Child1_url1.com", "URL2" : "https://Child1_url2.com", "URL3" : "https://Child1_url3.com" } "CHILD2" : { "URL1" : "https://Child2_url1.com", "URL2" : "https://Child2_url2.com", } "CHILD3" : { "URL1" : "https://Child3_url1.com",

}

} 要检索我正在使用以下代码的URL

override func viewDidLoad() { super.viewDidLoad() checkSectionCount() }

func checkSectionCount(){ Database.database().reference().observe(.value) { (snapShot: DataSnapshot) in

    let snapDict = snapShot.value as? NSDictionary
    let snapCnt = String(snapDict!.count)
    print("snapCnt --> \(snapCnt)")// This prints the number of child counts correctly

}

}

func numberOfSections(in collectionView: UICollectionView) -> Int { return snapCnt //I am unable to refer the snapCnt to set the number of section dynamically here

} 如上面的代码所示,我无法从函数“ checkSectionCount”中引用“ snapCnt”。我也尝试将这个值作为函数参数返回,但是swift无法从Database.database()。reference(){代码中识别出'snapCnt'

}

感谢任何人可以指导我完成有关如何基于Firebase数据库中可用的详细信息为集合视图动态设置节和行号的工作。

展开
收起
被纵养的懒猫 2019-09-24 13:21:26 564 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载