从 UserDefaults 检索时未找到值 [英] Value not found when retrieving from UserDefaults

查看:58
本文介绍了从 UserDefaults 检索时未找到值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到的确切error:

valueNotFound(Foundation.Data, Swift.DecodingError.Context(codingPath: [_PlistKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "wishData", intValue: nil), _PlistKey(stringValue): "Index 0", intValue: 0), CodingKeys(stringValue: "image", intValue: nil)], debugDescription: "Expected Data value but found null instead.",underlyingError: nil))

valueNotFound(Foundation.Data, Swift.DecodingError.Context(codingPath: [_PlistKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "wishData", intValue: nil), _PlistKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "image", intValue: nil)], debugDescription: "Expected Data value but found null instead.", underlyingError: nil))

详情:

我有两个自定义 strcuts :

愿望清单:

struct Wishlist: Codable {
var name: String
var image: UIImage
var wishes: [Wish]
var color: UIColor
var textColor: UIColor
var index: Int

enum CodingKeys: String, CodingKey {
    case name, image, wishData, color, textColor, index
}

init(name: String, image: UIImage, wishes: [Wish], color: UIColor, textColor: UIColor, index: Int) {
    self.name = name
    self.image = image
    self.wishes = wishes
    self.color = color
    self.textColor = textColor
    self.index = index
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)

    name = try values.decode(String.self, forKey: .name)
    wishes = try values.decode([Wish].self, forKey: .wishData)
    color = try values.decode(Color.self, forKey: .color).uiColor
    textColor = try values.decode(Color.self, forKey: .textColor).uiColor
    index = try values.decode(Int.self, forKey: .index)

    let data = try values.decode(Data.self, forKey: .image)
    guard let image = UIImage(data: data) else {
        throw DecodingError.dataCorruptedError(forKey: .image, in: values, debugDescription: "Invalid image data")
    }
    self.image = image
}

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    try container.encode(name, forKey: .name)
    try container.encode(wishes, forKey: .wishData)
    try container.encode(Color(uiColor: color), forKey: .color)
    try container.encode(Color(uiColor: textColor), forKey: .textColor)
    try container.encode(index, forKey: .index)
    try container.encode(image.pngData(), forKey: .image)
}
}

还有Wish

And Wish

struct Wish: Codable {
public var name: String
public var checkedStatus: Bool
public var link: String
public var price: String
public var note: String
public var image: UIImage

init(name: String, link: String, price: String, note: String, image: UIImage, checkedStatus: Bool) {
    self.name = name
    self.checkedStatus = checkedStatus
    self.link = link
    self.price = price
    self.note = note
    self.image = image
}

enum CodingKeys: String, CodingKey {
    case name, checkedStatus, link, price, note, image
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)

    name = try values.decode(String.self, forKey: .name)
    checkedStatus = try values.decode(Bool.self, forKey: .checkedStatus)
    link = try values.decode(String.self, forKey: .link)
    price = try values.decode(String.self, forKey: .price)
    note = try values.decode(String.self, forKey: .note)

    let data = try values.decode(Data.self, forKey: .image)
    guard let image = UIImage(data: data) else {
        throw DecodingError.dataCorruptedError(forKey: .image, in: values, debugDescription: "Invalid image data")
    }
    self.image = image
}

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    try container.encode(name, forKey: .name)
    try container.encode(checkedStatus, forKey: .checkedStatus)
    try container.encode(link, forKey: .link)
    try container.encode(price, forKey: .price)
    try container.encode(note, forKey: .note)
    try container.encode(image.pngData(), forKey: .image)
}
}

SettingUserDefaults 没有任何错误,但 getting 会触发 error above:

Setting to UserDefaults works without any error but getting fires the error above:

func getDataSourceArray() -> [Wishlist]? {
    if let data = self.value(forKey: Keys.dataSourceKey) as? Data {
        do {
            _ = try PropertyListDecoder().decode(Array < Wishlist > .self, from: data) as [Wishlist]
        } catch let error {
            print(error)
        }
        if let dataSourceArray =
            try? PropertyListDecoder().decode(Array < Wishlist > .self, from: data) as[Wishlist] {
                return dataSourceArray
            } 
    }
    return nil
}

我知道这可能会令人困惑,所以如果您有任何需要了解的信息,请告诉我.我被困在这里,不知道问题是什么..

I know this might be quite confusing so let me know if there is anything you need to know. Im stuck here and have no idea what the problem is..

推荐答案

图像数据为零

"image", intValue: nil)], debugDescription: "预期数据值,但发现为 null."

"image", intValue: nil)], debugDescription: "Expected Data value but found null instead."

所以在init(fromdecoder: Decoder)中改变代码抛出{

if let data = try? values.decodeIfPresent(Data.self, forKey: .image),let image = UIImage(data: data) {
     self.image = image
}

public var image: UIImage?

并在您的自定义初始化中执行

and in your custom init do

, image: UIImage? = nil


也改变


Also change

try container.encode(image.pngData(), forKey: .image)

if let img = image , let im = img.pngData() {
  try container.encode(im, forKey: .image)
}

这篇关于从 UserDefaults 检索时未找到值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆