【问题标题】:Unable to assign values to array outside of function scope. Swift and Firebase无法将值分配给函数范围之外的数组。 Swift 和 Firebase
【发布时间】:2018-01-20 23:30:56
【问题描述】:
class myChallengesController: UIViewController,UITableViewDelegate, UITableViewDataSource{
   ...
   var myChallenges = [challenge]() //Here i declare an empty array of type challenge
   ...

以下是我正在使用的功能,用于获取特定用户的所有挑战。

func getAllChallenges(){
        var challenges = NSDictionary()
        FIRDatabase.database().reference(withPath: "challenges").observeSingleEvent(of: .value, with: { (snapshot) in
            // getting all of the challenges.

            challenges = (snapshot.value as? NSDictionary)!
            for (_,value) in challenges{
                let test = value as! NSDictionary
                let tempChallenge = challenge()
                for (key,_) in test{
                    // print("key:\(key), value : \(val)")
                    switch(key as! String){

                    case "owner":
                        tempChallenge.owner = test[key] as? String
                        break
                    case "participants":
                        let tempArray = tempChallenge.convertStringToArray(participants: (test[key] as? String)!)
                        tempChallenge.participants = tempArray
                        break
                    case "title":
                        tempChallenge.title = test[key] as? String
                        break
                    case "bounty":
                        tempChallenge.bounty = test[key] as? String
                        break
                    case "details":
                        tempChallenge.details = test[key] as? String
                        break
                    case "location":
                        tempChallenge.location = test[key] as? String
                        break
                    case "subtitle":
                        tempChallenge.subtitle = test[key] as? String
                        break
                    case "duration":
                        tempChallenge.duration = test[key] as? String
                        break
                    case "challengeID":
                        tempChallenge.challengeID = test[key] as? String
                        break
                    default:
                        break
                    }

                }

在这里,我将每个挑战附加到 myChallenges 数组中。 如果我测试第一个挑战的打印数据,它就可以工作。

                self.myChallenges.append(tempChallenge)
                print("title: \(self.myChallenges[0].title)") -->this works
                print("\(self.myChallenges.count)")
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }

但是当我尝试访问添加到 myChallenges 数组中的挑战时,似乎没有添加任何数据。这是我访问添加到数组中的数据的地方。

//这是tableview cellForRowAt函数的内部

cell.jobPosition.text = myChallenges[0].subtitle

cell.timeRemaining.text = myChallenges[0].duration

cell.owner.text = myChallenges[0].owner

当上述代码运行时,我得到“索引超出范围”错误

【问题讨论】:

  • 您是否有可能在填充数组之前尝试从数组中读取?我假设由于该函数是异步的,单元格将尝试在第一次加载时(在数据返回之前)进行初始化。
  • 我认为可能是这样,我该如何纠正?
  • 改用myChallenges.first?.subtitle
  • 行得通,谢谢!但是如果我想遍历数组中的多个项目,我应该使用什么? indexPath.row 不起作用

标签: arrays swift firebase firebase-realtime-database


【解决方案1】:

您需要为UITableView 提供适当的数据源,并且可以选择委托。你会想做这样的事情:

// ViewController.viewDidLoad
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
tableView.dataSource = self

class MyCell: UITableViewCell {
    var jobPosition: UITextField!
    // TODO: configure cell
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myChallenges.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
        cell.jobPosition.text = myChallenges[indexPath.row].subtitle
        return cell
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多