ios - "Cannot override ' init ' which has been marked unavailable"防止覆盖空 init

标签 ios swift swift2

我有一种情况,我试图重写 NSError 来为我提供一个我将要重复使用的错误实例。

在我更新 Xcode 并转换为 Swift 2 之前,我的代码一直有效。

public class NAUnexpectedResponseTypeError: NSError {
    public convenience init() {
        let messasge = "The object fetched by AFNetworking was not of an expected type."
        self.init(
            domain: "MyDomain",
            code: 6782,
            userInfo: [NSLocalizedDescriptionKey: messasge]
        )
    }
}

编译器说 无法覆盖已标记为不可用的“init”。通过这样做,我能够破解它:

public class NAUnexpectedResponseTypeError: NSError {
    public class func error() -> NSError {
        let message = "The object fetched by AFNetworking was not of an expected type."
        return NAUnexpectedResponseTypeError(
            domain: "MyDomain",
            code: 6782,
            userInfo: [NSLocalizedDescriptionKey: message]
        )
    }
}

所以,我的问题是:

  1. 有没有办法在这种情况下添加一个空的 init 方法?
  2. 如果对 1 的回答是肯定的,出于某种原因这是不是一个坏主意?
  3. 我使用类方法的变通方法是否适合缓解此问题?

编辑:

我想出了另一个我更喜欢的解决方法,而不是使用类方法的解决方法。我仍然不高兴我不能覆盖空的 init 方法。

public class NAUnexpectedResponseTypeError: NSError {
    public convenience init(message: String?) {
        var errorMessage: String
        if let message = message {
            errorMessage = message
        } else {
            errorMessage = "The object fetched by AFNetworking was not of an expected type."
        }
        self.init(
            domain: "MyDomain",
            code: 6782,
            userInfo: [NSLocalizedDescriptionKey: errorMessage]
        )
    }
}

最佳答案

由于 NSError 是不可变的,因此没有理由为同一数据创建多个实例。只需创建一个常量实例:

let NAUnexpectedResponseTypeError = NSError(domain: "MyDomain",
    code: 6782,
    userInfo: [NSLocalizedDescriptionKey: "The object fetched by AFNetworking was not of an expected type."]
)

如果你遇到的情况不是常量,那么扩展 NSError 而不是子类几乎总是更好。例如:

extension NSError {
    class func MyError(code code:code, message: String) -> NSError {
        return NSError(domain: "MyDomain", 
                       code: code,
                       userInfo: [NSLocalizedDescriptionKey: message])
   }
}

这种扩展(作为一个类别)在 ObjC 中有很长的历史,并且是一个很好的模式,可以带到 Swift 中(如果你不能轻易使用 enum ErrorTypes,它会更好 swift )。

在很多情况下,我发现仅仅拥有一个顶层函数比扩展 NSError 更容易。例如:

private func makeError(code code:code, message: String) -> NSError {
    return NSError(domain: "MyDomain", 
                   code: code,
                   userInfo: [NSLocalizedDescriptionKey: message])
}

(就个人而言,当我必须使用 NSError 时,我总是在 Swift 中使用这些类型的函数。在 ObjC 中,我通常在 NSError 上使用类别。不知道为什么我改变了,但感觉更自然。)

关于ios - "Cannot override ' init ' which has been marked unavailable"防止覆盖空 init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32749209/

相关文章:

ios - 在 iOS 设备上以编程方式测试蓝牙版本

ios - 编程 .beginRefresh() 不刷新 UITableView 中的数据

ios - Swift 2 JSON POST 请求 [NSMutableURLRequest 的 HTTPBody 的字典与字符串]

swift2 - Swift 2 : is there a short syntax to conditionally set a non-optional variable from an optional variable?

ios - xcode 构建中的不同参数

ios - 如何在 Interface Builder 中使用 QLPreviewController?

ios - 将数据从 Objective-C Viewcontroller 传递到 Swift ViewController

swift - 覆盖所有模块的 Swift.print() 或共享功能

swift - 如何让两个 SKSpriteNode 的位置始终相同?

ios - 是否可以在 Swift 中覆盖 UIButton 的操作方法?