Swift 4从错误中获取错误代码

时间:2018-06-07 17:28:50

标签: ios swift

我有一个被定义为错误的变量,这就是我打印它时的样子:

ArrayList<Rent> rents = loadRents();
        String[] header = new String[] {"Star date", "End date", "Room number","Guest name", "Guest lastname", "Guest ID card"};
        Object[][] show = new Object[rents.size()][header.length];
        for (int i = 0; i < rents.size(); i++) {
            Rent r = rents.get(i);
            show[i][0] = r.getDateStart();
            show[i][1] = r.getDateEnd();
            show[i][2] = r.getRoomNumber();
            show[i][3] = r.getGuestName();
            show[i][4] = r.getGuestLastname();
            show[i][5] = r.getGuestIDCard();

            DefaultTableModel tableModel = new DefaultTableModel(show, header);
            tblRents = new JTable(tableModel);
            tblRents.setRowSelectionAllowed(true);
            tblRents.setColumnSelectionAllowed(true);
            tblRents.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            tblRents.setDefaultEditor(Object.class, null);
            tblRents.setAutoCreateRowSorter(true);

我想要做的是获得-2的代码...我该怎么做?

4 个答案:

答案 0 :(得分:4)

您可以先打开可选的error并比较-2案例。

if let error = error {
   switch error._code {
      case LAError.userCancel.rawValue: // or -2 if you want
        // do something
      default:
        break
   }
}

答案 1 :(得分:2)

您只需要将错误转换为LAError(本地身份验证错误)并切换其代码属性:

if let error = error as? LAError {
    switch error.code {
    case .userCancel:
        print("userCancel")
    default:
        print("unknown error")
    }
}

答案 2 :(得分:0)

我确定您要使用.border上的.border{ color: #E9C8FF; } 属性:

code

答案 3 :(得分:0)

我写了这个小扩展名:

extension Error {
    var errorCode:Int? {
        return (self as NSError).code
    }
}

使用:

if error.errorCode == -2 {
    //some code
}