2013-03-10 42 views
1

使用下面的代碼是我可以得到writeToFile實際保存文件的唯一方法。它輸出到這個目錄:用writeToFile保存NSString將只保存到一個位置

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

回答

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]); 
} 
+0

有關輸出的任何想法? 2013-03-09 21:42:10.421日曆小工具[2458:303]保存到/Users/Eric/Desktop/gadget.ics時出錯 - (null) – loopifnil 2013-03-10 05:43:18

+0

感謝您的快速響應! – loopifnil 2013-03-10 05:47:29

+0

我在上面的問題中看到「容器」一詞,我認爲這表示沙箱。嘗試轉換沙盒***關閉***(它在目標設置中),並查看寫入桌面是否有更好的結果。 – 2013-03-10 05:47:29