新手问个iphone程序的问题,困扰了很久了

2011-11-29 23:03:26 +08:00
 GhXst
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
fileName = [NSString stringWithFormat:@"%@/File.txt", documentsDirectory];
[self.data writeToFile:fileName atomically:NO];
其中
self.data的类型为NSMutableArray,用NSLog能看到self.data的内容,fileName也有,atomically:YES和NO都试了。程序没有报错,但是运行完打开指定文件夹没有文件生成,会是什么问题?
3727 次点击
所在节点    iDev
5 条回复
GhXst
2011-11-29 23:34:02 +08:00
没有人帮忙吗?还是我提供的信息不够全啊?
GhXst
2011-11-29 23:41:51 +08:00
好了,已经解决了,写文件成功,就是有些乱码,再调整了
[NSKeyedArchiver archiveRootObject:self.data toFile:fileName];
Livid
2011-11-29 23:44:15 +08:00
NSMutableArray 有 writeToFile:atomically: 这个方法么?
keakon
2011-11-30 00:00:35 +08:00
@GhXst 乱码和coder有关,只要你载入没问题,就不用管乱码了。拼接路径可以用stringByAppendingPathComponent:。
GhXst
2011-11-30 00:47:49 +08:00
@keakon 想不到一个小小的问题惊动老大了!测试如下:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
fileName = [NSString stringWithFormat:@"%@/File.txt", documentsDirectory];
NSMutableArray* test = [[NSMutableArray alloc] init];
NSString* a = @"This is a test a!";
NSString* b = @"This is a test b!";
[test addObject:a];
[test addObject:b];
[test writeToFile:fileName atomically:YES];

输出文件成功,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>This is a test a!</string>
<string>This is a test b!</string>
</array>
</plist>

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/22456

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX