gpt4 book ai didi

ios - 实现 UserDefaults 时遇到问题

转载 作者:行者123 更新时间:2023-12-04 14:50:28 26 4
gpt4 key购买 nike

我以前有编码经验,但我对 Swift 和 iOS 都非常陌生。我正在开发一个供个人使用的应用程序,以将我用来帮助管理 ADHD 的多个不同应用程序的功能合并到一个地方。该应用程序的主视图提供了一种跟踪日常支出的方法。我正在尝试使用 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文件中

//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] = []

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

您不需要saveStuffsaveBread

以下是您的代码的简化版本。我无法复制你的进行测试。

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 的好用处。它适用于较小的东西。

The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s defaults database.

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

如果您只使用 iOS 或其他数据库系统(如 Firebase、AWS、Azure 等),您可能需要研究 Core Data。

关于ios - 实现 UserDefaults 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69148980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com