最简单的写入文件,但是一直报出错
- (IBAction)buttonPressed
{
NSString *documentsDirectory= [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString* fileName = [documentsDirectory
stringByAppendingPathComponent:@"config.txt"];
NSString *message = [[NSString alloc] initWithFormat:
@"version:1 year:1982 month:12 day:25"];
[message writeToFile:fileName atomacally:YES];
[message release];
}每次到writetofile的时候就出错.
报的错误是
-[NSCFString writeToFile:atomacally:]: unrecognized selector sent to instance 0x4d208b0
- (IBAction)buttonPressed
{
NSString *documentsDirectory= [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString* fileName = [documentsDirectory
stringByAppendingPathComponent:@"config.txt"];
NSString *message = [[NSString alloc] initWithFormat:
@"version:1 year:1982 month:12 day:25"];
[message writeToFile:fileName atomacally:YES];
[message release];
}每次到writetofile的时候就出错.
报的错误是
-[NSCFString writeToFile:atomacally:]: unrecognized selector sent to instance 0x4d208b0
message?fileName?
分情况看⋯⋯
建议把 message和fileName都打印一下就知道什么原因了
@"version:1 year:1982 month:12 day:25"];有问题,直接写成
NSString *message = @"version:1 year:1982 month:12 day:25";
这个好了。要么是下面的:
NSString *message = [NSString stringWithFormat:
@"version:1 year:%@ month:%@ day:%@", @"1982",@"12",@"25"];
使用 [message writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:nil];
楼主你用的那个方法是NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);的,这种方法要避免使用。