Swift enum as parameter in function

I have a class that stores an enum and provides a function to display that enum as a string if the enumeration is specified as a parameter.

enum ErrorType: String {
    case InvalidInputTextFieldEmpty = "One or more inputs seem empty. Please enter your credentials."
    case InvalidInputPasswordsNotMatch = "Please check your passwords they doesn't seem to match."
}

class ErrorManager: NSObject {
    func handleError(errorType: ErrorType)
    {
        self.showAlertView(errorType.rawValue)
    }

    func showAlertView(message:String)
    {
        var alertView:UIAlertView = UIAlertView(title: "Notice", message: message, delegate: self, cancelButtonTitle: "OK")
        alertView.show()
    }
}

      

Now I want to access the handleError function in another class with: ErrorManager.handleError(ErrorType.InvalidInputTextFieldEmpty)

But the compiler complains that the parameter is n0t if kind is ErrorManager, although I wrote that the parameter is of the form ErrorType. What am I doing wrong here?

+3


source to share


1 answer


You are trying to access a method as a class method if you declared it as an instance method. You need to instantiate the ErrorManager class and use it as a receiver in a method call, or change method declarations as class methods, for example:



class ErrorManager: NSObject {
    class func handleError(errorType: ErrorType) {
        ErrorManager.showAlertView(errorType.rawValue)
    }

    class func showAlertView(message: String) {
        let alertView = UIAlertView(title: "Notice", message: message, delegate: self, cancelButtonTitle: "OK")
        alertView.show()
    }
}

      

+5


source







All Articles