【问题标题】:Codable NSManagedObject fail on decodeIfPresent Data typeCodable NSManagedObject 在 decodeIfPresent 数据类型上失败
【发布时间】:2017-11-28 11:59:52
【问题描述】:

我在尝试解码这个 json 时收到此错误:

[{
    "id": "76f22c25-cee7-4c7a-94fa-1fb85720f580",
    "purchaseDate": "2012-04-05T19:03:43Z",
    "title": "azare",

}, {
    "id": "9b4b9f7d-382f-4555-9eaa-97939b13633f",
    "purchaseDate": "2012-04-05T19:02:46Z",
    "title": "Chocolat",

}, {
    "id": "02a0aa06-2d0c-4ab9-aaaa-af7dee7b4845",
    "purchaseDate": "2012-09-24T17:39:52Z",
    "title": "Some thing",
}]

致命错误:“试试!”表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0)), MyApp.Purchase.CodingKeys.purchaseDate], debugDescription: "预期解码 Double 但找到一个字符串/数据 相反。”,基础错误:无)):文件 /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.69.2/src/swift/stdlib/public/core/ErrorType.swift, 第 181 行

My code:

//Purchase+CoreDataClass.swift
@objc(Purchase)
public class Purchase: NSManagedObject, Codable {

    enum CodingKeys: String, CodingKey {
        case id
        case title
        case purchaseDate
    }

required convenience public init(from decoder: Decoder) throws {

var context : NSManagedObjectContext = MyAppCoreDataManager.sharedInstance.persistentContainer.viewContext
        guard let entity = NSEntityDescription.entity(forEntityName: "Purchase", in: context!) else { fatalError() }

        self.init(entity: entity, insertInto: context!)
        let container = try decoder.container(keyedBy: CodingKeys.self)

        self.title = try container.decodeIfPresent(String.self, forKey: .title)
        self.id = try container.decodeIfPresent(String.self, forKey: .id)
        self.purchaseDate = try! container.decodeIfPresent(Date.self, forKey: .purchaseDate)

    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(title, forKey: .title)
try container.encode(purchaseDate, forKey: .purchaseDate)
}
}

//   Purchase+CoreDataProperties.swift

extension Purchase {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Purchase> {
        return NSFetchRequest<Purchase>(entityName: "Purchase")
    } 


    @NSManaged public var id: String?
    @NSManaged public var title: String?
    @NSManaged public var purchaseDate: Date?
}

//CALL

   let decoder = JSONDecoder()

                    decoder.dataDecodingStrategy = .base64
                    let purchases = try decoder.decode([Purchase].self, from: value)

If i remove "purchaseDate" everything works correctly

【问题讨论】:

  • 在 json 中 purchaseDate 是字符串格式,与数据模型中的数据类型 Date 不匹配。

标签: swift nsmanagedobject codable


【解决方案1】:

有一个普遍的误解,错误是关于Date而不是Data

出现该错误是因为 JSON 解码器默认不解码 ISO8601 日期。默认值为TimeInterval aka Double

这就是错误消息的内容:它需要一个Double,但找到了一个String

你必须将解码器的DateDecodingStrategy设置为.iso8601

decoder.dateDecodingStrategy = .iso8601

并去掉try!中的感叹号交出错误

【讨论】:

  • 'JSONDecoder.DataDecodingStrategy' 没有成员 'iso8601' @vadian
  • 再次是 DatE 而不是 DatApurchaseDate 是日期和时间的 ISO8601 表示,而不是二进制数据
  • 好的。但是decoder.dateDecodingStrategy = .iso8601 似乎是一个无效的选项
  • 选项是valid
  • 对不起。这是一个编译问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多