最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中NSKeyedArchiver.archiveRootObject失败

Swift crifan 3894浏览 0评论

代码:

    let key_conversationItemList = gCurUserItem.id "/conversationItemList"
    var conversationItemList:[ConversationItem]
        let saveOk = NSKeyedArchiver.archiveRootObject(conversationItemList, toFile: key_conversationItemList)
        print("saveOk=\(saveOk)")

返回为false

swift NSKeyedArchiver archiveRootObject false

swift – NSKeyedArchiver.archiveRootObject returns "false" if I want to overwrite data, why? – Stack Overflow

ios – NSKeyedArchiver archiveRootObject keeps returning NO – Stack Overflow

好像是要创建对应文件夹才可以。。。

Data persistence with NSCoding in swift for more information visit 

https://gist.github.com/tomdiggle/75906f60a485affaabd2

算了,还是去用:

nickoneill/Pantry

但是发现很难使用:

需要实现一堆的Storable

并且:

如果要实现的类中init包含自定义参数的话,还很难搞定。。

算了,还是使用NSKeyedUnarchiver吧。

How to save and load objects with NSKeyedArchiver and NSKeyedUnarchiver – Swift 2 example code

swift实现iOS数据持久化之归档NSKeyedArchiver – swift迷

Swift and NSCoding: Keeping it Simple

swift NSKeyedArchiver 示例

结果创建了文件夹后,调用

NSKeyedArchiver.archiveRootObject

还是false:

       let saveFileUrl = saveDirUrl.URLByAppendingPathComponent("conversationItemList")
        print("saveFileUrl=\(saveFileUrl)")
        //saveFileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C96626A4-93C7-4354-85D4-66272E1E4800/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList/
        let saveFilePath = saveFileUrl.path!
        print("saveFilePath=\(saveFilePath)")
        //saveFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C96626A4-93C7-4354-85D4-66272E1E4800/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList
        let saveOk = NSKeyedArchiver.archiveRootObject(conversationItemList, toFile: saveFilePath)
        //        let saveOk = Pantry.pack(conversationItemList, key: key_conversationItemList)
        print("saveOk=\(saveOk)") //saveOk=false

swift NSKeyedArchiver false

Writing swift dictionary to file – Stack Overflow

Swift NSCoding Not working – Stack Overflow

此处,解决了一个小问题:

保存的文件

从:

saveFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/2A46A702-E252-46A8-9D73-0E49994AD9EF/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList/

改为:

saveFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/2A46A702-E252-46A8-9D73-0E49994AD9EF/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList

后,是可以调用到对应的要保存的数组:

var conversationItemList:[ConversationItem]

中内部的:

class ConversationItem:NSObject, NSCoding {
    func encodeWithCoder(aCoder: NSCoder){

了。

但是最终结果还是出错了。。。

而去调试期间,也找不到:

内部到底是什么原因导致encode出错的。。。

【总结】

结果此处故意设置为,上一级目录中去保存数据,结果竟然可以成功保存:

//        let saveFileUrl = saveDirUrl.URLByAppendingPathComponent("conversationItemList", isDirectory: false)
        let saveFileUrl = gCurUserItem.archiveRootDir.URLByAppendingPathComponent("conversationItemList", isDirectory: false)
        print("saveFileUrl=\(saveFileUrl)")
        //saveFileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/2A46A702-E252-46A8-9D73-0E49994AD9EF/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList
        //file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/10710286-12D6-4963-9B56-7D7E3730BA29/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/conversationItemList
        let saveFilePath = saveFileUrl.path!
        print("saveFilePath=\(saveFilePath)")
        //saveFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/2A46A702-E252-46A8-9D73-0E49994AD9EF/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/Conversation/conversationItemList
        ///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/10710286-12D6-4963-9B56-7D7E3730BA29/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/conversationItemList
        //let saveOk = NSKeyedArchiver.archiveRootObject(conversationItemList, toFile: saveFilePath)
        let saveOk = NSKeyedArchiver.archiveRootObject(conversationItemList[0], toFile: saveFilePath)
        //        let saveOk = Pantry.pack(conversationItemList, key: key_conversationItemList)
        print("saveOk=\(saveOk)")
        //saveOk=false
        //saveOk=true

最后的最后,终于正常了:

可以正常保存数据了。。。

而之前出错的原因是:

1.第一次是由于,不小心把file文件的路径最后加上了/,变成了路径了。。所以保存就会出错

2.第二次是:由于之前的(调试的代码而导致的)误操作,创建了对应的同名的一个文件夹:

/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/10710286-12D6-4963-9B56-7D7E3730BA29/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/conversationItemList

所以后面再去用上面作为文件名去保存数据的话

-》相当于把数据写入已经存在的一个文件中(而不是文件中)

所以导致出错。。

转载请注明:在路上 » [已解决]Swift中NSKeyedArchiver.archiveRootObject失败

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.170 seconds, using 22.15MB memory