简体   繁体   English

将NSMutableString写入文件

[英]write NSMutableString to file

what is suppose to be wrong here with my code: 我的代码在这里应该错了什么:

-(void)writetofile:(NSMutableString *)str{
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/code.txt",path];

    NSStringEncoding *encode=NULL;

    [str writeToFile:filePath atomically:YES encoding:*encode error:nil];
}

try this: 尝试这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"code.txt"];
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

You are dereferencing a NULL pointer: 您正在取消引用NULL指针:

NSStringEncoding *encode = NULL;

`... encoding:*encode ...`

Instead, just use NSUTF8StringEncoding : 相反,只需使用NSUTF8StringEncoding

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM