简体   繁体   English

writeToFile:atomically:方法附加到当前存在的文件?

[英]writeToFile:atomically: method appends to currently exisiting file?

It seems like the writeToFile method appends to the currently existing file path as long as the method calls happen during the same application runtime. 只要方法调用在同一应用程序运行时期间发生,似乎writeToFile方法会附加到当前存在的文件路径。 Is this true? 这是真的?

Are there any other ways to ensure appending to an existing file while an application session is running? 有没有其他方法可以确保在应用程序会话运行时附加到现有文件? I would like the file to be overwritten on a new session. 我希望在新会话中覆盖该文件。

FYI, I am using an NSMutableDictionary as the receiver to the writeToFile message. 仅供参考,我使用NSMutableDictionary作为writeToFile消息的接收者。

To ensure the file is overwritten for each new session, simply delete the file when a new session starts. 要确保为每个新会话覆盖文件,只需在新会话启动时删除该文件。

To append to a file, you can use an NSOutputStream to write the data. 要附加到文件,可以使用NSOutputStream来写入数据。 Open the file for appending. 打开文件以进行追加。 But if you are writing a plist file from an NSDictionary , then this won't work. 但是,如果您正在从NSDictionary编写plist文件,那么这将无效。 The better solution would be to load the dictionary at app startup, update it in memory as the apps runs, and write the dictionary to a file when the app enters the background. 更好的解决方案是在app启动时加载字典,在应用程序运行时在内存中更新字典,并在应用程序进入后台时将字典写入文件。 This assumes your "session" isn't defined by app startups (or background/foreground transitions). 这假设您的“会话”不是由应用启动(或后台/前台转换)定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM