簡體   English   中英

無法為函數范圍之外的數組分配值。 Swift和Firebase

[英]Unable to assign values to array outside of function scope. Swift and Firebase

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

當上面的代碼運行時,出現“索引超出范圍”錯誤

您需要為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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM