首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iphone writeToFile:问题

iphone writeToFile:问题
EN

Stack Overflow用户
提问于 2011-06-23 06:01:16
回答 4查看 736关注 0票数 1

有没有办法将我的"/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..."文件保存到除.xml之外的另一个目录

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"accUserNames.xml"];
 BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
    NSLog(@"Error writing file !");
}

我想writeToFile:我的.xml文件到桌面上,知道怎么做吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-06-23 06:20:12

希望下面的代码能有所帮助,

代码语言:javascript
复制
NSString *documentsDirectory = @"/Users/student/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
    NSLog(@"Error writing file !");
}
票数 2
EN

Stack Overflow用户

发布于 2011-06-23 06:07:24

在iPhone模拟器中,您应该能够成功地使用@"/Users/student/Desktop/accUserNames.xml"作为写入路径。但是,您不能在iOS设备上这样做(您将被限制在应用程序的沙箱目录中-建议您写入其中的Documents文件夹或其他文件夹,这取决于您存储的数据类型)。

编辑:我想我理解你的问题。代码的这一部分:

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

有效地找到通往“/User/student/Library/Application Support/iPhone/Simulator/User/Applications/...”“的路径(这是你想要保存东西的正常地方)。但是,如果您只想暂时保存到桌面,则只需使用appFile = @"/Users/student/Desktop/accUserNames.xml"即可。

注意:我并不主张这是一个长期的解决方案,但是如果你只是想暂时看到你的程序的输出,它可以正常工作。

票数 1
EN

Stack Overflow用户

发布于 2011-06-23 06:08:48

可能没有使用代码,但您可以尝试这样做:打开"Automator“(在实用程序文件夹中)并选择”文件夹操作“。作为输入文件夹,您可以指定文档的目录(/User/student/Library/Application Support/iPhone/Simulator/User/Applications/...) )然后从“文件和文件夹”、“复制查找项”和“移动查找项”中选择,然后选择桌面。点击“保存”,给它一个名字,所有文件在文档文件夹应复制到桌面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6449929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档