【问题标题】:How to Append to Array of Structs and have Change Persistant?如何附加到结构数组并保持更改?
【发布时间】:2021-03-05 23:27:28
【问题描述】:

我希望能够将对象列表存储在单独的 Swift 文件中,并在页面中调用它们以显示它们。我用这段代码成功地做到了:

import Foundation
import SwiftUI



struct MatchInfo: Hashable, Codable {
    let theType: String
    let theWinner: String
    let theTime: String
    let id = UUID()
}


var matchInfo = [
MatchInfo(theType: "Capitalism", theWinner: "Julia", theTime: "3/3/2021"),
MatchInfo(theType: "Socialism", theWinner: "Julia", theTime: "3/2/2021"),
MatchInfo(theType: "Authoritarianism", theWinner: "Luke", theTime: "3/1/2021")
]

我在此处的另一个页面上播放比赛后附加到列表的位置:

 matchInfo.insert(MatchInfo(theType: typeSelection, theWinner: winnerName, theTime: "\(datetimeWithoutYear)" + "\(year)"), at: 0)

这是另一个页面上的一些代码,我将其称为列表:

List {
                    ForEach(matchInfo, id: \.self) { matchData in
                        
                        matchRow(matchData : matchData)
                        
                    } .background(Color("invisble"))
                    .listRowBackground(Color("invisble"))
                } .frame(height: 490)

...

struct matchRow: View {

let matchData: MatchInfo
 
var body: some View {
    HStack {
        VStack(alignment: .leading) {
            Text(matchData.theType)
                .font(.system(size: 20, weight: .medium, design: .default))
            Text("    Winner: " + matchData.theWinner)
        }
        Spacer()
    
        Text(matchData.theTime)
            .padding(.leading, 40)
            .multilineTextAlignment(.trailing)
    
    }
    .foregroundColor(.white)
    .accentColor(.white)
}
}

但此代码不会通过应用重新启动保存。我以前从未通过重新启动保存过任何东西,并且一直在努力寻找一个足够简单让我理解的答案。如何在下次打开应用时更新列表而不使其消失?

【问题讨论】:

    标签: swiftui swift5 swiftui-list


    【解决方案1】:

    好的,这里有一个关于如何保存到文档文件夹/从文档文件夹加载的示例。

    首先确保您的对象 MatchInfo 符合此协议。

    import Foundation
    
    protocol LocalFileStorable: Codable {
        static var fileName: String { get }
    }
    
    extension LocalFileStorable {
        static var localStorageURL: URL {
            guard let documentDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first else {
                fatalError("Can NOT access file in Documents.")
            }
            
            return documentDirectory
                .appendingPathComponent(self.fileName)
                .appendingPathExtension("json")
        }
    }
    
    extension LocalFileStorable {
        static func loadFromFile() -> [Self] {
            do {
                let fileWrapper = try FileWrapper(url: Self.localStorageURL, options: .immediate)
                guard let data = fileWrapper.regularFileContents else {
                    throw NSError()
                }
                return try JSONDecoder().decode([Self].self, from: data)
                
            } catch _ {
                print("Could not load \(Self.self) the model uses an empty collection (NO DATA).")
                return []
            }
        }
    }
    
    extension LocalFileStorable {
        static func saveToFile(_ collection: [Self]) {
            do {
                let data = try JSONEncoder().encode(collection)
                let jsonFileWrapper = FileWrapper(regularFileWithContents: data)
                try jsonFileWrapper.write(to: self.localStorageURL, options: .atomic, originalContentsURL: nil)
            } catch _ {
                print("Could not save \(Self.self)s to file named: \(self.localStorageURL.description)")
            }
        }
    }
    
    extension Array where Element: LocalFileStorable {
        ///Saves an array of LocalFileStorables to a file in Documents
        func saveToFile() {
            Element.saveToFile(self)
        }
    }
    

    您的主内容视图应如下所示:(我修改了您的对象以使其更简单。)

    import SwiftUI
    
    struct MatchInfo: Hashable, Codable, LocalFileStorable {
        static var fileName: String {
            return "MatchInfo"
        }
        
        let description: String
    }
    
    struct ContentView: View {
        @State var matchInfos = [MatchInfo]()
        
        var body: some View {
            VStack {
                Button("Add Match Info:") {
                    matchInfos.append(MatchInfo(description: "Nr." + matchInfos.count.description))
                    MatchInfo.saveToFile(matchInfos)
                }
                List(matchInfos, id: \.self) {
                    Text($0.description)
                }
                .onAppear(perform: {
                    matchInfos = MatchInfo.loadFromFile()
                })
            }
        }
    }
    

    【讨论】:

    • 谢谢你,我的意思是我可以成功地添加到我的列表并让它显示在其他地方,但不知道如何通过应用程序重新启动来保存它。你有没有机会告诉我我会怎么做?
    • 我更新了答案并添加了代码示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多