Reputation: 2629
I have a class which stores an enum and provides a function to display this enum as a string if the enum is given 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 function handleError in another class with:
ErrorManager.handleError(ErrorType.InvalidInputTextFieldEmpty)
But the compile complains that the parameter is n0t if kind ErrorManager although I wrote that the parameter is of kind ErrorType. What am I doing wrong here?
Upvotes: 3
Views: 5688
Reputation: 130222
You're attempting to access the method as a class method when you've declared it as an instance method. You either need to create an instance of your ErrorManager class and use that as the receiver in the method call, or change your method declarations to be class methods, like this:
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()
}
}
Upvotes: 5