我有一个类,它存储一个枚举,并提供一个函数,如果枚举作为参数给出,则将该枚举显示为字符串。
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()
}
}
现在我想访问另一个类中的函数 handleError:
ErrorManager.handleError(ErrorType.InvalidInputTextFieldEmpty)
但是编译会提示参数是 n0t if kind ErrorManager 尽管我写的参数是 ErrorType 类型。我在这里做错了什么?
最佳答案
当您将方法声明为实例方法时,您正试图将其作为类方法访问。您需要创建 ErrorManager 类的一个实例并将其用作方法调用中的接收者,或者将您的方法声明更改为类方法,如下所示:
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()
}
}
关于ios - Swift 枚举作为函数中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27877132/