简体   繁体   English

在从初始化程序返回之前,不会在所有路径上调用“self.init”

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

I'm working with CoreData and JSON parsing with Decodable following this and encountered an error at the end of the initializer saying '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")
        }
    }
    
}

Am I missing something?我错过了什么吗?

As it notes, you haven't called self.init on all paths.正如它所指出的,您尚未在所有路径上调用self.init For example, if context is nil, then you return without calling self.init .例如,如果context为 nil,则您无需调用self.init返回。

If you want to exit this initializer without creating an instance, you'll need to throw an error, not just return.如果你想在不创建实例的情况下退出这个初始化器,你需要抛出一个错误,而不仅仅是返回。

Since this init throws, it also doesn't make sense to catch errors and then discard them.由于这个init抛出,捕获错误然后丢弃它们也没有意义。 Just let them throw to the caller.只是让他们扔给调用者。

You should probably throw a custom error in guard statements instead of just returning.您可能应该在guard语句中抛出一个自定义错误,而不是仅仅返回。 Also you should remove do-catch from decoder function calls:您还应该从解码器函数调用中删除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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 json.swift 错误是 Self.init 未在委托初始化程序的所有路径上调用 - json.swift error is Self.init isn't called on all paths in delegating initializer 从初始化程序返回之前,并非在所有路径上都调用“ super.init” - 'super.init' isn't called on all paths before returning from initializer Swift:在从初始化程序返回之前没有在所有路径上调用“super.init”? - Swift: 'super.init' isn't called on all paths before returning from initializer? 在从初始化程序返回之前,不会在所有路径上调用super init - super init isn't called on all paths before returning from initializer 在从初始化程序错误返回之前,不会在所有路径上调用Super.init - Super.init isn't called on all paths before returning from initializer Error 解决“从初始化快速返回之前未在所有路径上调用Super.init的问题”。 - Problems getting around 'Super.init isn't called on all paths before returning from initializer swift.' 从初始化程序返回之前不会调用Super.init - Super.init isn't called before returning from initializer 在调用self.init之前自己使用的iOS Swift便利初始化程序 - iOS Swift convenience initializer self used before self.init called 在自定义UIGestureRecognizer类中调用self.init之前使用的self - Self used before self.init called in custom UIGestureRecognizer class 为什么我在UIButton的子类的便捷初始化中不能使用“ self.init(type:.custom)” - Why can't I use “self.init(type: .custom)” in convenience initializer at my subclass of UIButton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM