如何在协议中使用抛出函数的错误类型推断

时间:2018-02-28 02:07:55

标签: swift protocols

有没有办法创建一个具有指定Error子类型的抛出函数?下面的代码是我喜欢它的一个例子。

protocol ValidatorError: Error {
    var somePrintableThingy: String { get }
}

protocol Validator {
    associatedType T
    var model: T { get set }

    init(model: T)

    func validate() throws where Error: ValidatorError
}

示例用例

class SomeModelErrorNotValidatorError: Error {
    case invalidUsername
}

class SomeModelError: ValidatorError {
    case invalidUsername

    var somePrintableThingy: String {
        switch self { case .invalidUsername: return "haha" }
    }
}

class SomeModel {
    var username: String = ""
    init(username: String) { self.username = username }
}

class SomeModelValidator: Validator {
    var model: SomeModel

    init(model: SomeModel) { self.model = model }

    func validate() throws {
        guard self.model.username.isEmpty else {
            // this line should be fine
            throw SomeModelError.invalidUsername

            // if i replace it with this line it should not be fine
            throw SomeModelErrorNotValidatorError.invalidUsername
        }
    }
}

class SomeModelViewController: UIViewController {
    // .. some other codes here
    func buttonClicked(_ sender: Any? = nil) {
        let model = SomeModel(username: self.txtUsername.text ?? "")
        let validator = SomeModelValidator(model: model)

        do {
            try validator.validate()
        } catch let error { 
            // since I already know that this is a `SomeModelError`
            // I could just do this 
            print(error.somePrintableThingy)
        }
    }
}

PS:可能的解决方法是使用回调创建非投掷功能,但我不想这样做

PS PS:另一种可行的解决方法是使用linting工具,但这太过分了。

0 个答案:

没有答案