我构建一个 Mac 应用程序,我有 2 个问题:
- I want to create a text file to read and write data on it. I don't know how to crate a text file to read and write data. Is it use struct?
- I want to create a XML file to read and write data on it. Can I create a struct for XML?
你有什么建议吗?提前致谢
最佳答案
好吧,要创建一个文件,只需使用
[[NSFileManager defaultManager] createFileAtPath:@"Your/Path" contents:nil attributes:nil];
这将创建一个空文件,您可以写入或读取该文件。要写入文本(或 XML),只需使用 NSString
的 writeToFile:atomically:encoding:error:
方法,如下所示
NSString *str = //Your text or XML
[str writeToFile:"Your/Path" atomically:YES encoding:NSUTF8StringEncoding error:nil];
要从一个文件中读取,只需要用那个文件的内容创建一个NSString
NSString *contents = [NSString stringWithContentsOfFile:@"Your/Path"];
或者,如果它不包含字符串,则从文件中获取一个 NSData
对象
NSData *contents = [NSData dataWithContentsOfFile:@"Your/Path"];
关于objective-c - Objective-C - 创建文本文件以在 Cocoa 中逐行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779655/