在 Objective-C 中从 txt 文件中删除一行
有没有办法从 Objective-C 的 .txt 文件末尾删除一行?我似乎找不到任何关于从 Objective-C 操作文本文件的内容,只能将它们读入 NSString。
Is there a way to remove a line from the end of a .txt file from Objective-C? I can't seem to find anything on manipulating text files from Objective-C, only reading them into a NSString.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过将数据模型更改为 plist? Plist 更容易从 NSDictionaries 中读取/写入。
否则,我认为唯一的方法是将文件读入
NSString
,通过拆分\n
分成组件 NSArray,删除索引 n 处的对象,写回通过与组件\n
连接成字符串,然后写回文件。Have you considered changing your data model to a plist? Plists are more easily read/written into/from NSDictionaries.
Otherwise, I think the only way is to read the file into a
NSString
, separate into a component NSArray by splitting on\n
, remove the object at index n, write back into a string by joining with component\n
, then writing back to the file.