How do I save an NSString as a .txt file on my apps local documents directory?

前端 未结 5 679
孤街浪徒
孤街浪徒 2020-12-25 13:51

How do I save an NSString as a .txt file on my apps local documents directory (UTF-8)?

5条回答
  •  北海茫月
    2020-12-25 14:40

    Something like this:

    NSString *homeDirectory;
    homeDirectory = NSHomeDirectory(); // Get app's home directory - you could check for a folder here too.
    BOOL isWriteable = [[NSFileManager defaultManager] isWritableFileAtPath: homeDirectory]; //Check file path is writealbe
    // You can now add a file name to your path and the create the initial empty file
    
    [[NSFileManager defaultManager] createFileAtPath:newFilePath contents:nil attributes:nil];
    
    // Then as a you have an NSString you could simple use the writeFile: method
    NSString *yourStringOfData;
    [yourStringOfData writeToFile: newFilePath atomically: YES];
    

提交回复
热议问题