问题描述
我正在this之后使用Decodable处理CoreData和JSON解析,并且在初始化器的末尾说'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,则返回时不调用self.init
。
如果要退出此初始化程序而不创建实例,则需要抛出错误,而不仅仅是返回。
由于此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)
}