小编Nic*_*rge的帖子

SwiftUI:编码要保存在 AppStorage 中的结构

目前正在尝试在 swiftUI 中构建我的第一个应用程序。我认为最简单的部分会成为一场噩梦......在 AppStorage 中保存一个结构,以便在重新启动应用程序时可用

我有两个结构要保存。第一个是针对玩家的,我已经实现了RawRepresentable

struct Player: Codable, Identifiable {
    let id: Int
    let name: String
    let gamePlayed: Int
    let bestScore: Int
    let nbrGameWon: Int
    let nbrGameLost: Int
    let totalScore: Int?

}

typealias PlayerList = [Player]
extension PlayerList: RawRepresentable {
    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8),
            let result = try? JSONDecoder().decode(PlayerList.self, from: data)
        else {
            return nil
        }
        self = result
    }

    public var rawValue: String {
        guard let data = try? JSONEncoder().encode(self),
            let …
Run Code Online (Sandbox Code Playgroud)

encode swift swiftui

6
推荐指数
1
解决办法
4015
查看次数

标签 统计

encode ×1

swift ×1

swiftui ×1