我在文档目录中有文件,我想在这个文件中添加一些文本(它是一个JSON格式的字符串)。到目前为止,我使用下面的代码行写入文件,但每次写入文件时它都会覆盖文件。那么有更好的方法来处理这种情况吗?我是swift的新手,似乎无法找到解决方案。
stringVariable.writeToFile(self.directoryPath, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
因为它将是一个包含JSON字符串的文件,如何检查文本是否已经有一些数据并分配或删除"," (逗号)从Json String的结尾?
最初,该文件包含一些文本,如
{ "documentComments":[]}
我的JSON字符串如下所示,
{ "comment": "MySecondComment", "commentDate": "2015-04-23 17:58:59 +0000", "isSigned": 1, "accountId": 1, "documentId": 3 }
通过插入几个json字符串,我希望文件最终如下所示,
{ "documentComments":[
{ "comment": "MyFirstComment", "commentDate": "2015-04-22 17:58:59 +0000", "isSigned": 0, "accountId": 1, "documentId": 2 },
{ "comment": "MySecondComment", "commentDate": "2015-04-23 17:58:59 +0000", "isSigned": 1, "accountId": 1, "documentId": 3 }]}
先谢谢