在 iOS 上写入文件
如何在 iOS 上写入文件?我尝试使用以下代码来执行此操作,但我做错了:
char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"];
[data writeToFile:appFile atomically:YES];
我在资源上创建了 MyFile.txt。
How do I write a file on iOS? I'm trying to do it with the following code but I'm doing something wrong:
char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"];
[data writeToFile:appFile atomically:YES];
I have created MyFile.txt on resources.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许这对你有用。
May be this is useful to you.
你的代码在我这边工作正常,我刚刚测试过。您在哪里检查您的更改?使用文档目录路径。要获取路径 -
并从控制台复制路径,然后打开查找器并按 Cmd+shift+g 并将路径粘贴到此处,然后打开文件
Your code is working at my end, i have just tested it. Where are you checking your changes? Use Documents directory path. To get path -
and copy path from console and then open finder and press Cmd+shift+g and paste path here and then open your file
斯威夫特
斯威夫特 2
斯威夫特 3
Swift
Swift 2
Swift 3
尝试制作
为
Try making
as