目前正在尝试在 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)