xcode - 失败的初始化程序'init(fileURL :) cannot override a non-failable initializer

标签 xcode swift

我不确定如何在 Xcode 6.4 中修复此错误

override init?(fileURL url: NSURL) {
    super.init(fileURL: url)

    let center = NSNotificationCenter.defaultCenter()
    center.addObserver(self, selector: "thingsDidChange:", name: WhatsitDidChangeNotification, object: nil)
}

deinit {
    let center = NSNotificationCenter.defaultCenter()
    center.removeObserver(self)
}

最佳答案

您的 init?(_:) 函数中没有任何内容可能会失败并强制您返回 nil。只需删除 init 签名末尾的问号即可。

override init(fileURL url: NSURL) {
    super.init(fileURL: url)
    let center = NSNotificationCenter.defaultCenter()
    center.addObserver(self, selector: "thingsDidChange:", name: WhatsitDidChangeNotification, object: nil)
}

关于xcode - 失败的初始化程序'init(fileURL :) cannot override a non-failable initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685045/

相关文章:

ios - 如何将 Objective-C 中的这段代码翻译成 Swift

ios - Swift .writeToFile 不更新 mainBundle 中的 JSON 文件

ios - 什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true?

swift - 标题没有显示,每次我连接源和委托(delegate)时,程序都会崩溃,当我不连接它们时,它不会打印任何内容

ios - Swift 3.1 : String. Encoding(rawValue:) 在 iOS 应用程序中运行不正确

ios - Swift 2 RAC4 合并 UITextField 信号和 UISwitch 信号

swift - 嵌套类型数组 : Why Does the Compiler Complain?

Xcode 5 + Sparkle + 代码签名

ios - 在iOS调整 View 中,当且仅当文本字段隐藏在键盘后面时才应该移动,否则不需要调整。

ios - setvalue :forkey: not working, 应用程序崩溃