I have extension which I declared in separate swift file which I use to handle errors when user fill out registration information. But instead to return String in my debug are I want to use some alerts or imageViews to display depends on the error. The problem is that I'm not sure how can I pass IBOutlet or create alert in return section of this extension. For example if firstName is empty, red circle alert (imageView) will be displayed near the firstName textfield. Maybe my architecture of handling errors is wrong or maybe there is a way how to do it?
I would be very grateful if you give me a right direction where to find solution.
Here is the extension
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"
}
}
}
Here is my code where I use this extension
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)
}
}