Objective-C creating a text file with a string

30,594

Solution 1

//Method writes a string to a text file
-(void) writeToTextFile{
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                                                  documentsDirectory];
    //create content - four lines of text
    NSString *content = @"One\nTwo\nThree\nFour\nFive";
    //save content to the documents directory
    [content writeToFile:fileName 
                     atomically:NO 
                           encoding:NSStringEncodingConversionAllowLossy 
                                  error:nil];

}

Solution 2

You don't know if you're getting any errors because you're ignoring the returned YES/NO value of the -writeToFile:... method, and giving it no error pointer into which to record any possible failure. If the method returns NO, you'd check (and handle or present) the error to see what went wrong.

At a guess, the failure is due to the path you constructed. Try -stringByAppendingPathComponent: instead of -stringByAppendingString: ... this and its related methods properly handle paths.

The file probably is actually being created (ie, you might not be getting any errors after all). My guess is the file is created somewhere like "~/Desktopfile.txt" since your use of -stringByAppendingString: doesn't consider the string as slash-separated path. Check your home folder - I'll bet the file's there.

Share:
30,594
Admin
Author by

Admin

Updated on October 06, 2020

Comments

  • Admin
    Admin over 3 years

    I'm trying to create a text file with the contents of a string to my desktop. I'm not sure if I'm doing it right, I don't get errors but it doesn't work either...

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
    NSString *desktopDirectory=[paths objectAtIndex:0];
    NSString *filename = [desktopDirectory stringByAppendingString: @"file.txt"];
    [myString writeToFile:filename atomically:YES encoding: NSUTF8StringEncoding error: NULL];
    
  • Matt S.
    Matt S. almost 13 years
    What? 1.) You don't need to use a / if you do it the way he did. 2.) Where do you see a UIAlertView? I don't. 3.) this question was asked almost 2 years ago.
  • Max von Hippel
    Max von Hippel over 8 years
    ..Also why would we check out something to do with a "Desktop" on iOS? iOS has no such thing.
  • Felipe
    Felipe about 7 years
    Thanks, mate! Simple and easy to use.
  • Maxweel
    Maxweel about 7 years
    Note that NSStringEncodingConversionAllowLossy shouldn't be used there, it's not a string encoding type but it's an encoding option that happens to have the same value as NSASCIIStringEncoding (1)
  • Mike
    Mike almost 4 years
    I might have gone with NSString *fileName = [documentsDirectory stringByAppendingPathComponent: @"textfile.txt"];.