如何取消存档 swift 3 中的数据?
How to unarchive data in swift 3?
我有一个应用程序,我将 CNContact
的数组保存到 UserDefaults
中:
var contactsArray = [CNContact]()
let defaults = UserDefaults()
func (contact: CNContact){
contactsArray.append(contact)
let contactArrayArchive = NSKeyedArchiver.archivedData(withRootObject: contactArray) //archive the data
defaults.set(contactArrayArchive, forKey: "contactArray")
defaults.synchronize()
}
这会存档数据,以便将其保存到 defaults
。我的问题是如何将此数据转换回 viewDidLoad
中的 CNContact
数组。我在网上看到很多建议使用 NSKeyedArchiver.unarchiveObjectwithData
的答案,但是在 Xcode 中输入 swift 3,表示 .unarchiveObjectwithData
不是 [=19= 的成员].我一直在 swift 3 中寻找有关如何执行此操作的内容,但一直没有成功。我如何取消存档该值?
如果你按⇧⌘0打开文档并输入nsk(3个字符就够了)你会看到
要un归档你需要的东西 NSKeyedUnarchiver
您可以使用
NSKeyedUnarchiver.unarchiveObject(with: Data)
您已经使用了 NSKeyedArchiver 。您不能使用它来取消存档对象。
我有一个应用程序,我将 CNContact
的数组保存到 UserDefaults
中:
var contactsArray = [CNContact]()
let defaults = UserDefaults()
func (contact: CNContact){
contactsArray.append(contact)
let contactArrayArchive = NSKeyedArchiver.archivedData(withRootObject: contactArray) //archive the data
defaults.set(contactArrayArchive, forKey: "contactArray")
defaults.synchronize()
}
这会存档数据,以便将其保存到 defaults
。我的问题是如何将此数据转换回 viewDidLoad
中的 CNContact
数组。我在网上看到很多建议使用 NSKeyedArchiver.unarchiveObjectwithData
的答案,但是在 Xcode 中输入 swift 3,表示 .unarchiveObjectwithData
不是 [=19= 的成员].我一直在 swift 3 中寻找有关如何执行此操作的内容,但一直没有成功。我如何取消存档该值?
如果你按⇧⌘0打开文档并输入nsk(3个字符就够了)你会看到
要un归档你需要的东西 NSKeyedUnarchiver
您可以使用
NSKeyedUnarchiver.unarchiveObject(with: Data)
您已经使用了 NSKeyedArchiver 。您不能使用它来取消存档对象。