Reputation: 235
I have this code who create a text file inside my directory in iphone:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/file.txt",
documentsDirectory];
NSString *content = @"my first text";
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:fileName] == NO) {
[content writeToFile:fileName
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&error];
}else{
[content writeToFile:fileName
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&error];
}
the code was working perfectly until at a certain time it does not work anymore, and is returning the following error:
Error Domain=NSCocoaErrorDomain Code=517 "The operation couldn’t be completed. (Cocoa error 517.)" UserInfo=0x8c852b0 {NSFilePath=/Users/williamlima/Library/Application Support/iPhone Simulator/7.1/Applications/B5781B9E-3915-4656-83D4-5E0FB6486567/Documents/file.txt, NSStringEncoding=1}
How I can solve this problem?
Upvotes: 4
Views: 1049
Reputation: 11646
my two cents:
Ozgur fix works in Mojave 10.4 using objC in Xcode 10, solving the error.
Upvotes: 0
Reputation: 1453
You can try with UTF8Encoding
[content writeToFile:fileName
atomically:YES
encoding: NSUTF8StringEncoding
error:&error];
Upvotes: 5