2017-03-12 100 views
-1

我有一個擴展,我使用獨立的swift文件聲明瞭用戶在填寫註冊信息時處理錯誤。但是,而不是在我的調試返回字符串是我想使用一些警報或imageViews顯示取決於錯誤。問題是我不確定如何通過IBOutlet或在此擴展的返回部分創建警報。例如,如果firstName爲空,則會在firstName文本字段附近顯示紅圈警報(imageView)。也許我的處理錯誤的架構是錯誤的,或者有什麼辦法可以做到這一點?擴展錯誤處理程序

如果您給我一個正確的方向,找到解決辦法,我將不勝感激。

這裏是擴展

import UIKit 

enum RegistrationErrors: Error { 
    case invalidFirstName 
    case invalidLastName 
    case invalidCountry 
} 

extension RegistrationErrors: CustomStringConvertible { 
var description: String { 
    switch self { 
    case .invalidFirstName: 
     return "FirstName cannot be empty" 

    case .invalidLastName: 
     return "LastName cannot be empty" 

    case .invalidCountry: 
     return "Country cannot be empty" 
    } 
} 

}

這裏是我的代碼,我使用這個擴展

func registrationUser(firstName: String, lastName: String, country: String) throws -> (String, String, String) { 
    guard let firstName = firstNameTextField.text , firstName.characters.count != 0 else { 
     throw RegistrationErrors.invalidFirstName 
    } 

    guard let lastName = lastNameTextField.text , lastName.characters.count != 0 else { 
     throw RegistrationErrors.invalidLastName 
    } 

    guard let country = countryTextField.text , country.characters.count != 0 else { 
     throw RegistrationErrors.invalidCountry 
    } 

    return (firstName, lastName, country) 
} 

// MARK: Actions 

@IBAction func continueBtnTapped(_ sender: Any) { 

    do { 
     let (firstName, lastName, country) = try registrationUser(firstName: firstNameTextField.text!, lastName: lastNameTextField.text!, country: countryTextField.text!) 
     if let currentUser = FIRAuth.auth()?.currentUser?.uid { 
      DataService.instance.REF_BASE.child("users").child("profile").setValue(["firstName": firstName, "lastName": lastName, "country": country, "userId": currentUser]) 
      performSegue(withIdentifier: "toUsersList", sender: self) 
     } 
    } catch let error as RegistrationErrors { 
     print(error.description) 
    } catch { 
     print(error) 
    } 
} 
+0

相關:http://stackoverflow.com/questions/39176196/how-to-provide-a-localized-description-with-an-error-type-in​​-swift。 –

+0

@MartinR事實上,我正在考慮把它作爲一個副本。我沒有的唯一原因是我不完全確定這是否是OP所要求的。 – matt

回答

0

不是從Error導出錯誤,而是從LocalizedError派生它。這使您可以提供一個errorDescription屬性,如果你願意,你甚至可以稍後本地化它:

enum RegistrationErrors: LocalizedError { 
    case invalidFirstName 
    case invalidLastName 
    case invalidCountry 
    var errorDescription : String? { 
     switch self { 
     case .invalidFirstName: return NSLocalizedString(
      "First name cannot be empty", comment:"First name cannot be empty") 
     case .invalidLastName: return NSLocalizedString(
      "Last name cannot be empty", comment:"Last name cannot be empty") 
     case .invalidCountry: return NSLocalizedString(
      "Country cannot be empty", comment:"Country cannot be empty") 
     } 
    } 
} 

這將成爲錯誤的localizedDescription在支撐點:

do { 
    throw RegistrationErrors.invalidLastName 
} catch { 
    let problem = error.localizedDescription 
    // "Last name cannot be empty" 
    // ... now present your alert ... 
} 

這是一個通用的解決方案。沒有特殊的代碼是必要的; catch點不必知道錯誤的任何額外屬性,如果catch點是Objective-C代碼,它甚至可以工作,因爲本地化的描述只是傳遞到NSError版本中。

+0

非常感謝! – Sargot

0

你RegistrationErrors CustomStringConvertible擴展是不是一個壞主意。我想你會想在這個do..catch塊中向用戶顯示錯誤。

你可以趕上像特定的錯誤:

do { 
    ... try registerUser(...) 
} catch RegistrationErrors.invalidFirstName { 
    view.invalidFirstName() 
} catch ... 

您也可以捕捉到錯誤時,它是一個RegistrationError,就像你在做什麼,然後處理特定的錯誤在開關:

// ... 
catch let error as RegistrationErrors { 
    print(error) 
    switch error { 
    case .invalidFirstName: 
     view.invalidFirstName() 
    // ... 
    } 
+0

謝謝你的幫助! – Sargot