I need to write a string into a file. For that, my code is:
-(void)writeToFile:(NSString *)fileName: (NSString *)data {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:appFile atomically:YES];
}
I am calling this method like this
ownServices *obj = [[ownServices alloc]init];
[obj writeToFile:@"iphone.txt" :@"this is mahesh babu"];
but it didn’t write into the text file.
What’s the reason? Can anyone please help me.
Thank u in advance.
Firstly, you are calling your method strangely. Rename the method to
and call it like so:
Secondly,
writeToFile:atomically:is deprecated, usewriteToFile:atomically:encoding:error::This way, you also see what the error is.