【发布时间】:2017-06-28 23:11:27
【问题描述】:
我有一个函数,它可以进入 firebase 数据库并提取我存储的项目数量。当我在函数本身中打印变量 postCount 时它返回三个,但是当我在其他任何地方打印它时它只有零。它似乎没有更新全局值。
import UIKit
import Firebase
class ViewController: UIViewController, iCarouselDataSource, iCarouselDelegate {
var items: [Int] = []
var postCount = 0
@IBOutlet var carousel: iCarousel!
func getData() {
Database.database().reference().child("Posts").observeSingleEvent(of: .value, with: { (snapshot) in
if let dict2 = snapshot.value as? [String: AnyObject] {
self.postCount = dict2.count
}
//This prints 3
print(self.postCount)
})
}
override func awakeFromNib() {
super.awakeFromNib()
print(self.postCount)
for i in 0 ... 99 {
items.append(i)
}
}
override func viewDidLoad() {
super.viewDidLoad()
getData()
print(self.postCount)
carousel.type = .cylinder
}
func numberOfItems(in carousel: iCarousel) -> Int {
return postCount
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var label: UILabel
var itemView: UIImageView
//reuse view if available, otherwise create a new view
if let view = view as? UIImageView {
itemView = view
//get a reference to the label in the recycled view
label = itemView.viewWithTag(1) as! UILabel
} else {
//don't do anything specific to the index within
//this `if ... else` statement because the view will be
//recycled and used with other index values later
itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 400))
itemView.image = UIImage(named: "page.png")
itemView.layer.borderWidth = 10
itemView.contentMode = .center
label = UILabel(frame: itemView.bounds)
label.backgroundColor = .clear
label.textAlignment = .center
label.font = label.font.withSize(50)
label.tag = 1
itemView.addSubview(label)
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
label.text = "\(items[index])"
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing) {
return value * 1.1
}
return value
}
}
【问题讨论】:
-
您确定没有在“viewDidLoad”之前调用“awakeFromNib”吗?我不肯定,但它可能是 super.viewDidLoad() 的一部分。尝试将 super.viewDidLoad() 移动到 viewDidLoad() 的底部
标签: swift function methods global-variables