ios - 执行 UserDefaults 时遇到问题
问题描述
我以前有过编码经验,但我对 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] = []
您可以像写入常规数组一样写入它们
你不需要saveStuff
或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。
推荐阅读
- javascript - React Native 图片背景覆盖部分组件
- react-native - React native expo 和 express 服务器的 GET 请求权限错误
- python - 如果是熊猫数据框中的条件并提取列值
- html - 无法对齐文本
- c++ - 如何在 QT 中获取独立小部件的句柄?
- r - 如何创建一个弹出窗口供用户在 R Shiny 中输入信息?
- swiftui - 获取数据时的SwiftUI URLSession错误(typeMismatch)WEBAPI
- generics - 使用特征和“匹配”的通用数值函数
- python - 复制文件并为 List Python 中的每个文件创建一个副本
- r - 我想按以前的最近日期过滤我的数据框到给定日期