0

我的代码在这里假设有什么问题:

-(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];
}
4

2 回答 2

3

尝试这个:

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];
于 2012-03-23T13:27:29.067 回答
2

您正在取消引用 NULL 指针:

NSStringEncoding *encode = NULL;

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

相反,只需使用NSUTF8StringEncoding

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
于 2012-03-23T13:25:32.233 回答