在iOS上写一个文件 [英] Write a file on iOS
本文介绍了在iOS上写一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在iOS上编写文件?我正在尝试使用以下代码,但我做错了:
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];
我已经在资源上创建了MyFile.txt。
I have created MyFile.txt on resources.
推荐答案
你的代码在我的结尾工作,我刚刚测试过。你在哪里检查你的变化?使用文档目录路径。获取路径 -
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 -
NSLog(@"%@",documentsDirectory);
并从控制台复制路径,然后打开查找器,然后按Cmd + shift + g并粘贴路径,然后打开你的文件
and copy path from console and then open finder and press Cmd+shift+g and paste path here and then open your file
这篇关于在iOS上写一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文