Objective-C creating a text file with a string

后端 未结 3 1160
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 01:05

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...

3条回答
  •  隐瞒了意图╮
    2020-12-30 01:50

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

提交回复
热议问题