在 Swift 5 中保存天数数组
Save array of Days in Swift 5
所以我创建了一个 class 的一天和一杯饮料。我正在尝试跟踪您一天喝了多少酒,但我正在努力节省多天时间。我目前正在设法保存当天(当天喝醉的量),但我不知道如何保存超过一天。
我想保存所有日期的 Day 类型数组。我该怎么做?
这是我的一天class:
public class Day: NSObject {
var date: Date
var goalAmount: Drink
var consumedAmount: Drink
func saveDay() {
let formatting = DateFormatter()
formatting.dateFormat = "EEEE - dd/mm/yy"
UserDefaults.standard.set(formatting.string(from: date), forKey: "date")
UserDefaults.standard.set(goalAmount.amountOfDrink, forKey: "goal")
UserDefaults.standard.set(consumedAmount.amountOfDrink, forKey: "consumed")
}
func loadDay() {
let rawDate = UserDefaults.standard.value(forKey: "date") as? String ?? ""
let formatter = DateFormatter()
formatter.dateFormat = "EEEE - dd/mm/yy"
date = formatter.date(from: rawDate)!
goalAmount.amountOfDrink = UserDefaults.standard.float(forKey: "goal")
consumedAmount.amountOfDrink = UserDefaults.standard.float(forKey: "consumed")
}
}
这是我的饮料class:
class Drink: NSObject {
var typeOfDrink: String
var amountOfDrink: Float
}
我在当天有任何更改时调用 saveDay(),然后在应用程序打开时调用 loadDay()。
1) 您需要为此创建对象数组:
goalAmount = [Drink]()
var date = [Date]()
并追加每个新元素。
您还可以在饮料中添加日期变量 class.
2) 你也可以创建字典数组:
var userData = [String : Any]()
key 将是你的约会对象,Any 中包含与饮料相关的数据,你可以存储 Anything。
更好的方法是将 class 的对象存储在 userDefaults 中,而不是存储 class 的特定属性。并使用 [Date]
而不是 Date
来节省多天
首先,您需要 Serialize 将对象存储在 userDefaults 中,并 Deserialize 从 userDefaults 中获取数据。
import Foundation
class Day: Codable {
var date = Date()
var goalAmount: Drink
var consumedAmount: Drink
init(date: Date, goalAmount: Drink,consumedAmount: Drink ) {
self.date = date
self.goalAmount = goalAmount
self.consumedAmount = consumedAmount
}
static func saveDay(_ day : [Day]) {
do {
let object = try JSONEncoder().encode(day)
UserDefaults.standard.set(object, forKey: "days")
} catch {
print(error)
}
}
static func loadDay() {
let decoder = JSONDecoder()
if let object = UserDefaults.standard.value(forKey: "days") as? Data {
do {
let days = try decoder.decode([Day].self, from: object)
for day in days {
print("Date - ", day.date)
print("Goal Amount - ", day.goalAmount)
print("Consumed Amount - ",day.consumedAmount)
print("----------------------------------------------")
}
} catch {
print(error)
}
} else {
print("unable to fetch the data from day key in user defaults")
}
}
}
class Drink: Codable {
var typeOfDrink: String
var amountOfDrink: Float
init(typeOfDrink: String,amountOfDrink: Float ) {
self.typeOfDrink = typeOfDrink
self.amountOfDrink = amountOfDrink
}
}
使用 saveAndGet()
方法从 userDefaults 中存储和获取详细信息
func saveAndGet() {
// use any formats to format the dates
let date = Date()
let goalAmount = Drink(typeOfDrink: "Water", amountOfDrink: 5.0)
let consumedAmount = Drink(typeOfDrink: "Water", amountOfDrink: 3.0)
let day1 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day2 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day3 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day4 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let days = [day1, day2, day3, day4]
Day.saveDay(days)
Day.loadDay()
}
所以我创建了一个 class 的一天和一杯饮料。我正在尝试跟踪您一天喝了多少酒,但我正在努力节省多天时间。我目前正在设法保存当天(当天喝醉的量),但我不知道如何保存超过一天。
我想保存所有日期的 Day 类型数组。我该怎么做?
这是我的一天class:
public class Day: NSObject {
var date: Date
var goalAmount: Drink
var consumedAmount: Drink
func saveDay() {
let formatting = DateFormatter()
formatting.dateFormat = "EEEE - dd/mm/yy"
UserDefaults.standard.set(formatting.string(from: date), forKey: "date")
UserDefaults.standard.set(goalAmount.amountOfDrink, forKey: "goal")
UserDefaults.standard.set(consumedAmount.amountOfDrink, forKey: "consumed")
}
func loadDay() {
let rawDate = UserDefaults.standard.value(forKey: "date") as? String ?? ""
let formatter = DateFormatter()
formatter.dateFormat = "EEEE - dd/mm/yy"
date = formatter.date(from: rawDate)!
goalAmount.amountOfDrink = UserDefaults.standard.float(forKey: "goal")
consumedAmount.amountOfDrink = UserDefaults.standard.float(forKey: "consumed")
}
}
这是我的饮料class:
class Drink: NSObject {
var typeOfDrink: String
var amountOfDrink: Float
}
我在当天有任何更改时调用 saveDay(),然后在应用程序打开时调用 loadDay()。
1) 您需要为此创建对象数组:
goalAmount = [Drink]()
var date = [Date]()
并追加每个新元素。 您还可以在饮料中添加日期变量 class.
2) 你也可以创建字典数组:
var userData = [String : Any]()
key 将是你的约会对象,Any 中包含与饮料相关的数据,你可以存储 Anything。
更好的方法是将 class 的对象存储在 userDefaults 中,而不是存储 class 的特定属性。并使用 [Date]
而不是 Date
来节省多天
首先,您需要 Serialize 将对象存储在 userDefaults 中,并 Deserialize 从 userDefaults 中获取数据。
import Foundation
class Day: Codable {
var date = Date()
var goalAmount: Drink
var consumedAmount: Drink
init(date: Date, goalAmount: Drink,consumedAmount: Drink ) {
self.date = date
self.goalAmount = goalAmount
self.consumedAmount = consumedAmount
}
static func saveDay(_ day : [Day]) {
do {
let object = try JSONEncoder().encode(day)
UserDefaults.standard.set(object, forKey: "days")
} catch {
print(error)
}
}
static func loadDay() {
let decoder = JSONDecoder()
if let object = UserDefaults.standard.value(forKey: "days") as? Data {
do {
let days = try decoder.decode([Day].self, from: object)
for day in days {
print("Date - ", day.date)
print("Goal Amount - ", day.goalAmount)
print("Consumed Amount - ",day.consumedAmount)
print("----------------------------------------------")
}
} catch {
print(error)
}
} else {
print("unable to fetch the data from day key in user defaults")
}
}
}
class Drink: Codable {
var typeOfDrink: String
var amountOfDrink: Float
init(typeOfDrink: String,amountOfDrink: Float ) {
self.typeOfDrink = typeOfDrink
self.amountOfDrink = amountOfDrink
}
}
使用 saveAndGet()
方法从 userDefaults 中存储和获取详细信息
func saveAndGet() {
// use any formats to format the dates
let date = Date()
let goalAmount = Drink(typeOfDrink: "Water", amountOfDrink: 5.0)
let consumedAmount = Drink(typeOfDrink: "Water", amountOfDrink: 3.0)
let day1 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day2 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day3 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let day4 = Day(date: date, goalAmount: goalAmount, consumedAmount: consumedAmount)
let days = [day1, day2, day3, day4]
Day.saveDay(days)
Day.loadDay()
}