【问题标题】:Saving NSString with writeToFile will only save to one location用 writeToFile 保存 NSString 只会保存到一个位置
【发布时间】:2013-03-10 05:34:51
【问题描述】:

使用下面的代码是我可以让writeToFile 实际保存文件的唯一方法。它输出到这个目录:

/Users/Eric/Library/Containers/net.ericmann.Event-Gadget-Workshop-App/Data/Documents/gadget.ics

- (IBAction)Saved:(id)sender { 
    [self writeToTextFile];
    //mySaved.stringValue = [NSString stringWithFormat:@"Saved!"];
}


-(void) writeToTextFile{
    //get the documents directory:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    //NSString *pathToDesktop = [NSString stringWithFormat:@"/Users/%@/Desktop", NSUserName()];


    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/gadget.ics", documentsDirectory];

    //create content - four lines of text
    NSString *content = myOutput.stringValue;

    //save content to the documents directory
    [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
    mySaved.stringValue = fileName;
    myTestBox.stringValue = fileName;
}

现在,如果我使用下面的代码,什么都不会写。我有测试文件名输出,它指向这里:

/Users/Eric/Desktop/gadget.ics

-(void) writeToTextFile{
    //get the documents directory:

    // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);

    //NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    //NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *pathToDesktop = [NSString stringWithFormat:@"/Users/%@/Desktop", NSUserName()];

    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/gadget.ics", pathToDesktop];

    //create content - four lines of text
    NSString *content = myOutput.stringValue;

    //save content to the documents directory
    [content writeToFile:fileName atomically:NO  encoding:NSStringEncodingConversionAllowLossy error:nil];
    mySaved.stringValue = fileName;
    myTestBox.stringValue = fileName;

【问题讨论】:

    标签: objective-c nsstring writetofile


    【解决方案1】:

    如果您真的使用writeToFile”方法中的错误参数,您最终可能会找到解决方案:

    例如:

    NSError * error = NULL;
    BOOL success = [content writeToFile:fileName atomically:NO encoding:NSUTF8StringEncoding error:&error];
    if(success == NO)
    {
        NSLog( @"error saving to %@ - %@", fileName, [error localizedDescription] );
    }
    

    【讨论】:

    • 对输出有什么想法吗? 2013-03-09 21:42:10.421 日历小工具 [2458:303] 错误保存到 /Users/Eric/Desktop/gadget.ics - (null)
    • 感谢您的快速回复!
    • 我在您的问题中看到“容器”一词,我认为这表示沙盒。尝试关闭(在目标设置中)关闭沙盒,看看写入桌面是否有更好的结果。
    • 你摇滚。就是这样。我已经向 Mac App Store 提交了一个版本(需要开启沙盒)。谢谢! :-)
    • 是的,你是对的@Suge; “NSStringEncodingConversionAllowLossy”对原始代码无效,我从问题代码中盲目复制。从理论上讲,loopifnil 已经抓住了这一点并处理了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多