在 struct codable identifiable 上更新一个值,并且只有用户默认缓存这个值
Update a value on stuct codable identifiable and only userdefault cache this value
我有 2 个可编码的结构:Student 和 Adress(链接到 Student)
在我的应用程序中,我从 Firebase RTDB 获取数据,然后使用 userdefault 存储它
假设一个学生更改了他的电子邮件,我只想用更新后的电子邮件更新存储的 userdefault。
当我想存储它时是否需要指定所有其他数据(姓名、地址、出生日期...)或者我是否可以只 update/store 我的 userdefault 上的电子邮件而不指定其他数据 ?
struct Adress : Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_Street: String
var Student_Country: String
var Student_City: String
enum CodingKeys: String, CodingKey {
case Student_UID = "Adress_Student_UID"
case Student_Street = "Student_Street"
case Student_Country = "Student_Country"
case Student_City = "Student_City"
}
}
struct Student_Profile_Data:Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_firstName: String?
var Student_username : String?
var Student_Email : String?
var Student_lastName: String
var Student_Address: Adress?
var Student_DOB: String?
var Student_Studentpoints : Int?
enum CodingKeys: String, CodingKey {
case Student_UID = "Student_UID"
case Student_firstName = "Student_firstName"
case Student_username = "Student_username"
case Student_Email = "Student_Email"
case Student_lastName = "Student_lastName"
case Student_Address = "Student_Address"
case Student_DOB = "Student_DOB"
case Student_Studentpoints = "Student_Studentpoints"
}
}
The USERDEfault part:
//READ
NSLog("userdefault TEST READ")
let defaults = UserDefaults.standard
if let savedStudent = defaults.object(forKey: "SavedStudent") as? Data {
let decoder = JSONDecoder()
if let loadedStudent = try? decoder.decode(Student_Profile_Data.self, from: savedStudent) {
NSLog("TEST PROFILE- username : \(loadedStudent.Student_username)")
}}
//WRITE
let add1 = Adress(Student_UID: "", Student_Street: "", Student_Country: "", Student_City: "")
let stud = Student_Profile_Data(Student_firstName: "", Student_username: "Martin", Student_Email: "", Student_lastName: "", Student_Address: add1, Student_DOB: "", Student_Studentpoints: 0, Student_UID: "")
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(stud) {
let defaults = UserDefaults.standard
defaults.set(encoded, forKey: "SavedStudent")
NSLog("WRITE OK »)
}
//UPDATE ONE (OR TWO) VALUES ?
如果您以这种方式将学生存储在 UserDefault
中,那么对其进行更新将涉及 three-step 读取学生、更新其值,然后将其写回的过程。
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=10=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
savedStudent.firstName = "John"
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
为了更方便,您可以将其提取为函数:
func updateSavedStudent(updateBlock: (inout StudentProfileData) -> Void) {
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=11=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
updateBlock(&savedStudent)
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
// usage:
updateSavedStudent {
[=11=].firstName = "John"
[=11=].lastName = "Smith"
}
或者,为这个保存的学生计算 属性 并将其放入某个实用程序 class 中。请注意,这将为您每更新一次 属性 对学生进行一次编码和解码。
static var savedStudent: StudentProfileData {
get {
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
return defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=12=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
}
set {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(newValue) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
}
// usage
savedStudent.firstName = "John"
我有 2 个可编码的结构:Student 和 Adress(链接到 Student)
在我的应用程序中,我从 Firebase RTDB 获取数据,然后使用 userdefault 存储它 假设一个学生更改了他的电子邮件,我只想用更新后的电子邮件更新存储的 userdefault。
当我想存储它时是否需要指定所有其他数据(姓名、地址、出生日期...)或者我是否可以只 update/store 我的 userdefault 上的电子邮件而不指定其他数据 ?
struct Adress : Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_Street: String
var Student_Country: String
var Student_City: String
enum CodingKeys: String, CodingKey {
case Student_UID = "Adress_Student_UID"
case Student_Street = "Student_Street"
case Student_Country = "Student_Country"
case Student_City = "Student_City"
}
}
struct Student_Profile_Data:Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_firstName: String?
var Student_username : String?
var Student_Email : String?
var Student_lastName: String
var Student_Address: Adress?
var Student_DOB: String?
var Student_Studentpoints : Int?
enum CodingKeys: String, CodingKey {
case Student_UID = "Student_UID"
case Student_firstName = "Student_firstName"
case Student_username = "Student_username"
case Student_Email = "Student_Email"
case Student_lastName = "Student_lastName"
case Student_Address = "Student_Address"
case Student_DOB = "Student_DOB"
case Student_Studentpoints = "Student_Studentpoints"
}
}
The USERDEfault part:
//READ
NSLog("userdefault TEST READ")
let defaults = UserDefaults.standard
if let savedStudent = defaults.object(forKey: "SavedStudent") as? Data {
let decoder = JSONDecoder()
if let loadedStudent = try? decoder.decode(Student_Profile_Data.self, from: savedStudent) {
NSLog("TEST PROFILE- username : \(loadedStudent.Student_username)")
}}
//WRITE
let add1 = Adress(Student_UID: "", Student_Street: "", Student_Country: "", Student_City: "")
let stud = Student_Profile_Data(Student_firstName: "", Student_username: "Martin", Student_Email: "", Student_lastName: "", Student_Address: add1, Student_DOB: "", Student_Studentpoints: 0, Student_UID: "")
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(stud) {
let defaults = UserDefaults.standard
defaults.set(encoded, forKey: "SavedStudent")
NSLog("WRITE OK »)
}
//UPDATE ONE (OR TWO) VALUES ?
如果您以这种方式将学生存储在 UserDefault
中,那么对其进行更新将涉及 three-step 读取学生、更新其值,然后将其写回的过程。
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=10=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
savedStudent.firstName = "John"
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
为了更方便,您可以将其提取为函数:
func updateSavedStudent(updateBlock: (inout StudentProfileData) -> Void) {
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=11=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
updateBlock(&savedStudent)
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
// usage:
updateSavedStudent {
[=11=].firstName = "John"
[=11=].lastName = "Smith"
}
或者,为这个保存的学生计算 属性 并将其放入某个实用程序 class 中。请注意,这将为您每更新一次 属性 对学生进行一次编码和解码。
static var savedStudent: StudentProfileData {
get {
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
return defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: [=12=])
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
}
set {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(newValue) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
}
// usage
savedStudent.firstName = "John"