在 iOS 上写文件 [英] Write a file on iOS

查看:26
本文介绍了在 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.

推荐答案

你的代码在我这边有效,我刚刚测试过了.你在哪里检查你的变化?使用 Documents 目录路径.获取路径 -

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);

并从控制台复制路径,然后打开finder并按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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆