嘿,我无法访问我的全局数组,因为它是空的。我知道我已经在此链接How to access global array in Swift 4?
中提出了这个问题但是他们没有在代码中显示它,所以我不知道如何实现它。谁能告诉我该怎么做?这是我的代码
var prayers = [Prayer]() //This is my global array
func fetchPrayer() {
guard let city = self.city else { return}
self.prayers = []
APIServices.shared.fetchPrayer(name: city) { (results) in
results.datetime.forEach({ (time) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: time.times.Fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: time.times.Dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: time.times.Asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: time.times.Maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: time.times.Isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.updateUI()
self.tableView.reloadData()
}
}
}
这是我的表格视图代码,我将func setupTableView放入viewDidLoad
extension PrayerController: UITableViewDelegate, UITableViewDataSource {
func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: self.cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
}
//MARK:- TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return prayers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! PrayerTimeViewCell
let prayer = prayers[indexPath.row]
cell.prayer = prayer
return cell
}
}
这是代码的错误索引超出范围的地方
func updateUI() {
let prayer0 = prayers[0].prayerTime
let prayer1 = prayers[1].prayerTime
let prayer2 = prayers[2].prayerTime
let prayer3 = prayers[3].prayerTime
let prayer4 = prayers[4].prayerTime
}
答案 0 :(得分:1)
首先,您如何确定在致电prayers
时updateUI
阵列中有五个祈祷?
第二,应避免可能并发访问全局变量。我建议按以下方式修改您的代码:
APIServices.shared.fetchPrayer(name: city) { (results) in
var newPrayers = [Prayer]()
results.datetime.forEach({ (time) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: time.times.Fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: time.times.Dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: time.times.Asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: time.times.Maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: time.times.Isha)
newPrayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.prayers = newPrayers
self.updateUI()
self.tableView.reloadData()
}
}
在这里,您可以在newPrayers
闭包中创建本地fetchPrayer
数组。填充数组,完成后,在主线程中,然后通过将self.prayers
分配给新数组来“发布”结果。
尽管如此,在updateUI
中,您应明确检查数组中元素的数量是否正确。