gpt4 book ai didi

ios - 如何使用 NSKeyedArchiver(或其他机制)以 XML 格式将自己的类列表保存在 plist 中?

转载 作者:行者123 更新时间:2023-11-28 13:13:20 25 4
gpt4 key购买 nike

我设法将二进制格式的 plist 写入磁盘。但是我没有设法以 XML 格式编写数据。我读到这应该是可能的,但没有示例代码或文档这样做。

存储有效的代码:

var listofbookmarks = [Bookmark]() // <- needed to create a mutable array instance
listofbookmarks.append(Bookmark(name: "myname", position: "100"))

let fileManager = NSFileManager.defaultManager()

if let directories = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
if !directories.isEmpty {
let plistpath = directories[0].stringByAppendingPathComponent("BookmarkArray.plist")
if !fileManager.fileExistsAtPath(plistpath) {
// HOWTO TO STORE IN XML ?
NSKeyedArchiver.archiveRootObject(listofbookmarks, toFile: plistpath)

println(plistpath);
}
}
}

最佳答案

假设您的 Bookmark 类采用 NSCoding 协议(protocol),您所要做的就是将 NSKeyedArchiver outputFormat 设置为 NSPropertyListFormat.XMLFormat_v1_0:

替换

NSKeyedArchiver.archiveRootObject(listofbookmarks, toFile: plistpath)

let archivedData = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: archivedData)
archiver.outputFormat = NSPropertyListFormat.XMLFormat_v1_0
archiver.encodeObject(listofbookmarks)
archiver.finishEncoding()
archivedData.writeToFile(plistpath, atomically: true)

关于ios - 如何使用 NSKeyedArchiver(或其他机制)以 XML 格式将自己的类列表保存在 plist 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376952/

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