使用 @AppStorage 的字典扩展

awct

我知道在使用带有@AppStorage 的字典时我需要Dictionary遵守。RawRepresentable下面是我得到的最远的。

extension Dictionary: RawRepresentable where Key == String, Value == String {
    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8),  // convert from String to Data
            let result = try? JSONDecoder().decode([String:String].self, from: data)
        else {
            return nil
        }
        self = result
    }

    public var rawValue: String {
        guard let data = try? JSONEncoder().encode(self),   // data is  Data type
              let result = String(data: data, encoding: .utf8) // coerce NSData to String
        else {
            return "[:]"  // empty Dictionary respresented as String
        }
        return result
    }

}

扩展编译。但是当我声明一个空字典时它不起作用:

@AppStorage private var data :  [String:String] = [:]

错误信息是Missing argument for parameter #2 in call

代码有什么问题?

烧伤

您缺少存储的字符串键:

@AppStorage("data") private var data :  [String:String] = [:]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AppStorage与CoreData

字典的Python“扩展”

SwiftUI:什么是@AppStorage属性包装器

在字典扩展(Swift)中使用未声明的类型'KeyType'

快速可变字典的扩展

如何对字典进行扩展,以允许使用带有动态键的下标?

Snakemake使用字典扩展

使用点表示法扩展字典

如何在Python中使用键扩展字典中的值

使用@AppStorage进行字符串映射

Chrome扩展程序错误:不允许使用逗号结尾,并且必须引用字典键

使用@AppStorage更改SwiftUI暗/亮模式

SwiftUI中的可模拟@AppStorage

使用可选的初始化程序将自定义数据类型存储在@AppStorage中吗?

如何在SwiftUI中使用@AppStorage存储EnumTypes

扩展字典以包含词频

如何创建字典扩展

如何使用扩展名解码 JSON 嵌套字典数组:可解码?

SwiftUI:@AppStorage 使用中的“属性初始值设定项在‘self’可用之前运行”

如何使用 SwiftUI 使用 @AppStorage 存储浮点数

SwiftUI - AppStorage 不适用于 GeometryReader

如何使用@AppStorage 在 SwiftUI 中保存 UUID

将 SwiftUI 与 @AppStorage 和动态函数调用一起使用

Javascript:如何使用带有{键:数组值}的字典扩展数组而不将[对象]记录到控制台

@AppStorage 不更新视图

使用扩展运算符更新作为对象数组的字典值

SwiftUI @AppStorage 并不总是保持价值

使用同一字典中的键扩展 dict 值

SwiftUI iOS - 使用带有条件和 AppStorage 的 NavigationLink 时出现错误