麻烦实现UserDefaults

2022-01-28 13:17:55 标签 iosswiftswiftui

我以前有编程经验,但我对Swift和iOS都是新手。我正在开发一款个人使用的应用程序,将我用来帮助管理多动症的多个不同应用程序的功能整合到一个地方。应用程序的主视图提供了一种跟踪日常支出的方式。我试图使用UserDefaults来存储输入的信息,所以当我重新打开应用程序时,它仍然会在那里。应用程序运行良好,就像我可以弄清楚我已经写的数据处理正确,但它只是不工作。在过去的几天里,包括在这个网站上,我一直在敲打我的头撞墙,所以任何帮助都将非常感谢。下面是应用程序主视图的代码:

    import SwiftUIFontIcon
import SwiftUI
//import UIKit
struct ContentView: View {
    
    @State public var purchases = [Purchases]()
    
    @State public var prices = [Price]()
    
    @State public var isActive = false
    
    @State public var goTo: String = ""
    
    @State public var purchase: String = ""
    
    @State public var price: String = ""
    
    //    @State public var isActive: Bool = false
    
    init(){
        if let data = UserDefaults.standard.data(forKey: "Purchases"){
            if let decoded = try? JSONDecoder().decode([Purchases].self, from: data){
                self.purchases = decoded
                
            }
            return
        }
        self.purchases = []
        if let data2 = UserDefaults.standard.data(forKey: "Bread"){
            if let decoded2 = try? JSONDecoder().decode([Price].self, from: data2){
                self.prices = decoded2
                
            }
            return
        }
        self.prices = []
        
    }
    
    func addItem(){
        
        saveStuff()
        self.purchases.append(Purchases(name: purchase))
        saveStuff()
        purchase = ""
        
    }
    func addPrice(){
        saveBread()
        self.prices.append(Price(name: price))
        saveBread()
        price = ""
        
    }
    func deleteItem(at offsets: IndexSet){
        purchases.remove(atOffsets: offsets)
    }
    func deletePrice(at offsets: IndexSet){
        prices.remove(atOffsets: offsets)
    }
    func saveStuff(){
        if let encodedData = try? JSONEncoder().encode(purchases){
            UserDefaults.standard.set(encodedData, forKey: "Purchases")
        }
        //    return
    }
    
    func saveBread(){
        if let encodedData = try? JSONEncoder().encode(prices){
            UserDefaults.standard.set(encodedData, forKey: "Bread")
        }
        //    return
    }
    func clearList(){
        self.prices.removeAll()
        self.purchases.removeAll()
    }
    
    var body: some View {
        
        NavigationView{
            ZStack {
                Color.gray
                    .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                VStack {
                    //                    Spacer()
                    //                    HStack {
                    //                        //                                    Spacer()
                    //                        NavigationLink(
                    //                            destination: ToDoList(rootIsActive: self.$isActive),
                    //                            isActive: self.$isActive
                    //
                    //                        ){
                    //                            FontIcon.text(.ionicon(code: .ios_list_box), fontsize: 48, color: .black)
                    //                        }
                    //                        Spacer()
                    //                        NavigationLink(destination: ReminderView()){
                    //                            FontIcon.text(.ionicon(code: .ios_warning), fontsize: 48, color: .black)
                    //                        }
                    //                        //                                            Spacer()
                    //                    }
                    Spacer()
                    VStack{
                        Spacer()
                        HStack {
                            Spacer()
                            HStack {
                                Spacer()
                                TextField("Add an Item", text: $purchase)
                                    .padding(12)
                                    .border(Color.black)
                                Spacer()
                                Spacer()
                                Spacer()
                                TextField("Add a Price", text: $price)
                                    .padding(12)
                                    .border(Color.black)
                                //                                Spacer()
                                
                                FontIcon.button(.ionicon(code: .ios_add_circle), action: {
                                    addItem()
                                    addPrice()
                                    saveStuff()
                                    saveBread()
                                }, padding: 12, fontsize: 45, color: .black)
                            }
                            .opacity(1)
                            .padding(12)
                            //                            .border(Color.black)
                            //                            Spacer()
                        }
                        Spacer()
                        VStack {
                            HStack {
                                Spacer()
                                List{
                                    ForEach(purchases){ purchase in
                                        Text(purchase.name)
                                            .cornerRadius(16)
                                            .padding(10)
                                    }
                                    
                                    .onDelete(perform: deleteItem)
                                    //                                    .listStyle(GroupedListStyle())
                                    
                                }
                                Spacer()
                                List{
                                    ForEach(prices){ price in
                                        Text(price.name)
                                            .cornerRadius(16)
                                            .padding(10)
                                    }
                                    .onDelete(perform: deletePrice)
                                    //                                    .onAppear{UITableView.appearance().separatorColor = .clear}
                                }
                                Spacer()
                                
                            }
                            Spacer()
                            HStack {
                                //                                Spacer()
                                //                                FontIcon.button(.ionicon(code: .ios_save), action: {
                                //                                   saveStuff()
                                //                                    saveBread()
                                //                                }, padding: 12, fontsize: 78, color: .green)
                                Spacer()
                                
                                FontIcon.button(.ionicon(code: .ios_trash),action:{
                                    clearList()
                                }, padding: 12, fontsize: 78, color: .red)
                                Spacer()
                                NavigationLink(destination: ViewLists()){
                                    FontIcon.text(.ionicon(code: .ios_filing), fontsize: 58, color: .black)
                                }
                                Spacer()
                                NavigationLink(destination: ViewTotals()){
                                    FontIcon.text(.ionicon(code: .ios_add), fontsize: 58, color: .black)
                                }
                                Spacer()
                                NavigationLink(destination: IncomeView()){
                                    FontIcon.text(.ionicon(code: .ios_musical_note), fontsize: 58, color: .black)
                                    
                                }
                                Spacer()
                                
                            }
                            //                            Spacer()
                        }
                        Spacer()
                    }
                    Spacer()
                }
                .navigationBarTitle("ADD ToolKit", displayMode: .large)
                Spacer()
                //                    .navigationBarTitle("Ledger", displayMode: .large)
                //                Spacer()
            }
            .toolbar{
                ToolbarItem(placement: .primaryAction){
                    //                                                Spacer()
                    //                                            Spacer()
                    HStack {
                        Spacer()
                        NavigationLink(destination: ReminderView()){
                            FontIcon.text(.ionicon(code: .ios_warning), fontsize: 48, color: .black)
                            
                            //                                            Spacer()
                        }
                    }
                    
                }
                ToolbarItem(placement: .navigationBarLeading){
                    HStack {
                        
                        Spacer()
                        NavigationLink(
                            destination: ToDoList(rootIsActive: self.$isActive),
                            isActive: self.$isActive
                            
                        ){
                            FontIcon.text(.ionicon(code: .ios_list_box), fontsize: 48, color: .black)
                        }
                        
                        
                    }
                    
                }
                
            }
            
            
            
            //
            
            
            
            
            
            
            
        }
        
        .background(
            NavigationLink(destination: Text(self.goTo), isActive: $isActive){
                EmptyView()
            })
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

以及定义所有数组结构的模型代码:

import Foundation
import SwiftUI
struct Purchases: Identifiable, Codable{
    let id: String
    let name: String
    
    init(id: String = UUID().uuidString, name: String){
        self.id = id
        self.name = name
    }
}
struct Price: Identifiable, Codable{
    let id: String
    let name: String
    
    init(id: String = UUID().uuidString, name: String){
        self.id = id
        self.name = name
    }
}
struct ToDo: Identifiable, Codable{
    let id: String
    let name: String
    
    init(id: String = UUID().uuidString, name: String){
        self.id = id
        self.name = name
    }
}
struct Reminder: Identifiable, Codable{
    let id: String
    let name: String
    
    init(id: String = UUID().uuidString, name: String){
        self.id = id
        self.name = name
    }
}
struct Income: Identifiable, Codable{
    let id: String
    let name: String
    
    init(id: String = UUID().uuidString, name: String){
        self.id = id
        self.name = name
    }
}

谢谢提前

###将下面的代码添加到一个.swift file in your project

在项目中创建文件

//Allows all Codable Arrays to be saved using AppStorage
extension Array: RawRepresentable where Element: Codable {
    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8),
              let result = try? JSONDecoder().decode([Element].self, from: data)
        else {
            return nil
        }
        self = result
    }
    public var rawValue: String {
        guard let data = try? JSONEncoder().encode(self),
              let result = String(data: data, encoding: .utf8)
        else {
            return "[]"
        }
        return result
    }
}

然后对你想保存的数组使用@AppStorage vs @State

@AppStorage("Purchases") var purchases: [Purchases] = []
@AppStorage("Bread") var breadPrices: [Price] = []

你可以像写常规数组一样写它们

你不需要savesuff或saveBread

下面是您的代码的简化版本。我不能复制你的来测试。

import SwiftUI
struct CodableUserDefaultView: View {
    
    @AppStorage("Purchases") var purchases: [Purchases] = []
    //@State public var purchases = [Purchases]()
    @AppStorage("Bread") var breadPrices: [Price] = []
    //@State public var breadPrices = [Price]()
    
    @State public var isActive = false
    
    @State public var goTo: String = ""
    
    
    @State public var price: String = ""
    
    func addItem(purchaes: Purchases){
        
        self.purchases.append(purchaes)
        
    }
    func addPrice(price: Price){
        self.breadPrices.append(price)
        
    }
    func deleteItem(at offsets: IndexSet){
        purchases.remove(atOffsets: offsets)
    }
    func deletePrice(at offsets: IndexSet){
        breadPrices.remove(atOffsets: offsets)
    }
    
    func clearList(){
        self.breadPrices.removeAll()
        self.purchases.removeAll()
    }
    
    var body: some View {
        List{
            Section(content: {
                ForEach(breadPrices){ price in
                    HStack{
                        Text(price.name)
                        Spacer()
                        Button("purchase", action: {
                            addItem(purchaes: Purchases(name: price.name))
                        })
                    }
                }.onDelete(perform: deletePrice)
                VStack{
                    Text("Bread")
                    TextField("Bread Price", text: $price, onCommit: {
                        addPrice(price: Price(name: price))
                    })
                }
            }, header: {
                Text("Bread")
            })
            Section(content: {
                ForEach(purchases){ purchase in
                    HStack{
                        Text(purchase.name)
                    }
                }.onDelete(perform: deleteItem)
            }, header: {
                Text("Purchases")
            })
        }
    }
}
struct CodableUserDefaultView_Previews: PreviewProvider {
    static var previews: some View {
        CodableUserDefaultView()
    }
}

但是正如注释中提到的,这并不是UserDefaults的一个好用途。它是用来做小事情的。

默认系统允许应用定制自己的行为来匹配用户的偏好。例如,您可以允许用户指定他们首选的测量单位或媒体播放速度。应用程序通过在用户的默认数据库中为一组参数赋值来存储这些首选项。

https://developer。apple。com/documentation/foundation/userdefaults

如果你只使用iOS或者Firebase AWS Azure等其他数据库系统,你可能需要查看Core Data。

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码