'self.init' 在从初始值设定项返回之前没有在所有路径上调用

'self.init' isn't called on all paths before returning from initializer

我正在使用 CoreData 并 JSON 在 之后使用 Decodable 进行解析,并在初始化程序结束时遇到错误 'self.init' isn't called on all paths before returning from initializer:

import Foundation
import CoreData

extension CodingUserInfoKey {
    static let managedObjectContext = CodingUserInfoKey(rawValue: "managedObjectContext")!
}

@objc(Task)
public class Task: NSManagedObject, Decodable {
    
    enum CodingKeys: String, CodingKey {
        case diff, title, desc, doc
    }
    
    required convenience public init(from decoder: Decoder) throws {
        guard let context = decoder.userInfo[CodingUserInfoKey.managedObjectContext] as? NSManagedObjectContext else {
            print("Decode context error")
            return
        }
        guard let entity = NSEntityDescription.entity(forEntityName: "Task", in: context) else {
            print("Decode entity error")
            return
        }
        self.init(entity: entity, insertInto: context)
        do {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            self.diff = try container.decode(String.self, forKey: .diff)
            self.title = try container.decode(String.self, forKey: .title)
            self.desc = try container.decode(String.self, forKey: .desc)
            self.doc = try container.decode(String.self, forKey: .doc)
        } catch {
            print("Decode key error")
        }
    }
    
}

我是不是漏掉了什么?

正如它所指出的,您没有在所有路径上调用 self.init。例如,如果 context 为 nil,则您 return 无需调用 self.init.

如果您想在不创建实例的情况下退出此初始化程序,则需要抛出错误,而不仅仅是 return。

由于这个 init 抛出,捕获错误然后丢弃它们也没有意义。就让他们扔给来电者吧。

您可能应该在 guard 语句中抛出一个自定义错误,而不仅仅是返回。您还应该从解码器函数调用中删除 do-catch

enum ManagedObjectError: Error {
    case decodeContextError
    case decodeEntityError
}

required convenience public init(from decoder: Decoder) throws {
    guard let context = decoder.userInfo[CodingUserInfoKey.managedObjectContext] as? NSManagedObjectContext else {
        throw ManagedObjectError.decodeContextError
    }
    guard let entity = NSEntityDescription.entity(forEntityName: "Task", in: context) else {
        throw ManagedObjectError.decodeEntityError
    }
    self.init(entity: entity, insertInto: context)
    
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.diff = try container.decode(String.self, forKey: .diff)
    self.title = try container.decode(String.self, forKey: .title)
    self.desc = try container.decode(String.self, forKey: .desc)
    self.doc = try container.decode(String.self, forKey: .doc)
}