V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
GhXst
V2EX  ›  iDev

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

  •  
  •   GhXst · 2011-11-29 23:03:26 +08:00 · 3719 次点击
    这是一个创建于 4533 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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都试了。程序没有报错,但是运行完打开指定文件夹没有文件生成,会是什么问题?
    5 条回复    1970-01-01 08:00:00 +08:00
    GhXst
        1
    GhXst  
    OP
       2011-11-29 23:34:02 +08:00
    没有人帮忙吗?还是我提供的信息不够全啊?
    GhXst
        2
    GhXst  
    OP
       2011-11-29 23:41:51 +08:00
    好了,已经解决了,写文件成功,就是有些乱码,再调整了
    [NSKeyedArchiver archiveRootObject:self.data toFile:fileName];
    Livid
        3
    Livid  
    MOD
       2011-11-29 23:44:15 +08:00
    NSMutableArray 有 writeToFile:atomically: 这个方法么?
    keakon
        4
    keakon  
       2011-11-30 00:00:35 +08:00
    @GhXst 乱码和coder有关,只要你载入没问题,就不用管乱码了。拼接路径可以用stringByAppendingPathComponent:。
    GhXst
        5
    GhXst  
    OP
       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>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   877 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:42 · PVG 05:42 · LAX 14:42 · JFK 17:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.