gpt4 book ai didi

ios - 如何在 NSUserDefaults 中使用(多种类型)保存数组

转载 作者:可可西里 更新时间:2023-11-01 00:35:35 25 4
gpt4 key购买 nike

这很简单,但似乎无法找到正确的信息来解决在用户默认值中保存这样的数组。

它说这不是 NSUser Defaults Excepts 的属性。

代码:

    var notificationList: [(type: String,imageName: String, text: String, date: String, seen: Bool)] = [(type: "Default",imageName: "ClearPartioned", text: "", date: "", seen: true)]


if (UserDefaults.standard.object(forKey: "notificationList")) == nil { // first time launching

print("making notification list")

UserDefaults.standard.set(notificationList, forKey: "notificationList")
UserDefaults.standard.synchronize()

print("\(notificationList)")

} else {

print("getting saved array")

notificationList = (UserDefaults.standard.object(forKey: "notificationList") as! [(type: String, imageName: String, text: String, date: String, seen: Bool)])

print("\(notificationList)")
}

enter image description here

更新:

这更接近,但给出了在这个问题 here 中发现的错误.这些是我能够找到的最隐秘的答案,它们要么已经过时,要么会导致系统崩溃。

代码:

    if (UserDefaults.standard.object(forKey: "notificationList")) == nil { // first time launching

print("making notification list")

let encodedData = NSKeyedArchiver.archivedData(withRootObject: notificationList)
UserDefaults.standard.set(encodedData, forKey: "notificationList")
UserDefaults.standard.synchronize()

} else {

print("getting saved array")

notificationList = (UserDefaults.standard.object(forKey: "notificationList") as! [(type: String, imageName: String, text: String, date: String, seen: Bool)])

print("\(notificationList)")
}

更新 2:这是来自 Dhiru 的最佳答案实现

代码:

  if (UserDefaults.standard.object(forKey: "notificationList")) == nil { // first time launching
print("making notification list")

let notificationData = NSKeyedArchiver.archivedData(withRootObject: notificationList)
UserDefaults.standard.set(notificationData, forKey: "notificationList")
UserDefaults.standard.synchronize()

} else {

print("getting saved array")

let decodedData = UserDefaults.standard.object(forKey: "notificationList") as! Data
let notificationList = NSKeyedUnarchiver.unarchiveObject(with: decodedData) as AnyObject

print("\(notificationList)")
}

它给我一个导致系统崩溃的错误

   *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x1c011f380'
libc++abi.dylib: terminating with uncaught exception of type NSException

我确信这段代码会修复它,但由于我不知道如何使用这段代码,所以执行起来很糟糕,下面有多个错误。

代码:

   func (coder aDecoder: NSCoder) {
if let notificationList = aDecoder.decodeObjectForKey("notificationList") {
self.notificationList = notificationList
}
}

func encodeWithCoder(aCoder: NSCoder) {
if let notificationList = notificationList {
aCoder.encodeObject(notificationList, forKey: "notificationList")
}
}

最佳答案

您必须以数据的形式存储您的对象转换成数据使用NSKeyedArchiver.archivedData(withRootObject:)

转换回对象使用 NSKeyedUnarchiver.unarchiveObject(with:)

为 UserDefaults 保存数据

let notificationData = NSKeyedArchiver.archivedData(withRootObject: notificationList)
UserDefaults.standard.set(notificationData, forKey: "notificationList")

从 User UserDefaults 中检索数据

let decodedData  = UserDefaults.standard.object(forKey: "notificationList") as! Data
let notificationList = NSKeyedUnarchiver.unarchiveObject(with: decodedData) as! AnyObject

关于ios - 如何在 NSUserDefaults 中使用(多种类型)保存数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44940033/

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