多个选取器视图:在展开Optional值时意外发现nil

时间:2017-06-01 01:06:54

标签: ios swift uipickerview

我正在尝试使用以下代码创建一个包含多个pickerviews的视图控制器:

    values = new int[]{1,2,3,4,5};
    String query =
        "SELECT film_id, title\n"
        + "FROM film\n"
        + "WHERE film_Id IN (";
    for (int i = 0; i < values.length; ++i) {
        if (i > 0) {
            query += ",";
        }
        query += "?";
    }
    query += ")";

    try {
        Connection conn = DriverManager.getConnection("db-url", "user", "pwd");
        PreparedStatement stmt = conn.prepareStatement(query);
        for (int i = 0; i < values.length; ++i)
            stmt.setInt(i+1, values[i]);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()){
            // process results
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

然而,我收到一个错误:

  

致命错误:在解包可选值时意外发现nil

在我的import UIKit import Firebase import FirebaseAuth class DormViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { @IBOutlet weak var dormName: UIPickerView! @IBOutlet weak var entryway: UIPickerView! @IBOutlet weak var roomNumber: UIPickerView! @IBOutlet weak var nextButton: UIButton! var ref: DatabaseReference! let dorms = ["Adams", "Apley", "Cabot", "Canaday", "Currier", "DeWolfe 10", "Dewolfe 20", "Dunster", "Eliot", "Fairfax", "Grays", "Greenough", "Hollis", "Holworthy", "Hurlbut", "Inn at Harvard", "Kirkland", "Leverett Towers", "Leverett McKinlock Hall", "Lionel", "Lowell", "Massachusetts Hall", "Mather Lowrise", "Mather Tower", "Matthews", "Mower", "New Quincy", "Pennypacker", "Pforzheimer", "Pforzheimer Jordan North", "Pforzheimer Jordan South", "Stone Hall", "Stoughton", "Straus", "Thayer", "Weld", "Wigglesworth", "Winthrop" ] let entrances = ["N/A", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] let rooms = ["N/A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"] override func viewDidLoad() { super.viewDidLoad() ref = Database.database().reference() dormName = UIPickerView() entryway = UIPickerView() roomNumber = UIPickerView() dormName.delegate = self dormName.dataSource = self entryway.delegate = self entryway.dataSource = self roomNumber.delegate = self roomNumber.dataSource = self dormName.tag = 0 entryway.tag = 1 roomNumber.tag = 2 // Do any additional setup after loading the view. } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { if pickerView.tag == 1 { return dorms[row] } else if pickerView.tag == 2 { return entrances[row] } else { return rooms[row] } } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if pickerView.tag == 1 { return dorms.count } else if pickerView.tag == 2 { return entrances.count } else { return rooms.count } } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { let user = Auth.auth().currentUser if let user = user { let uid = user.uid var entry = entrances[row] var num = rooms[row] if entrances[row] == "N/A" { entry = "" } if rooms[row] == "N/A" { num = "" } self.ref.child("Dorms").child(uid).setValue(["Location": dorms[row] + " " + entry + "-" + num ]) } else { print("error") } } @IBAction func nextButton(_ sender: Any) { performSegue(withIdentifier: "tohomepage", sender: self) } } 功能中,当我拨打电话时:

viewDidLoad()

我尝试删除了委托和dataSource调用,但仍然会发生同样的错误。我还尝试删除标签并按名称引用选择器视图,如:

dormName.delegate = self

然后我收到 pickerview == "dormName" 错误。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

当你从xib或storyboad拖动UIPickerView时,你不需要调用一些init:

dormName = UIPickerView()
entryway = UIPickerView()
roomNumber = UIPickerView()

删除它,然后重试

答案 1 :(得分:0)

didSelectRow中,您不再检查选择器实例,只需使用相同的row索引访问所有阵列的数据。一旦您选择索引超出最小数组长度的项目(在本例中为entrances),这将导致索引超出范围错误。

另外,看起来你的标签/索引错了。例如。您将tag的{​​{1}}设置为0,但在代码中,当dormName等于1(实际上是指tag选择器)时,您可以访问宿舍数据。