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

前端 未结 5 701
孤街浪徒
孤街浪徒 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:19

    He is how to save NSString into Documents folder. Saving other types of data can be also realized that way.

    - (void)saveStringToDocuments:(NSString *)stringToSave {
    
     NSString *documentsFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
     NSString *fileName = [NSString stringWithString:@"savedString.txt"];
    
     NSString *path = [documentsFolder stringByAppendingPathComponent:fileName];
    
     [[NSFileManager defaultManager] createFileAtPath:path contents:[stringToSave dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    }
    

提交回复
热议问题