1

I'm trying to store a file in the NSApplicationSupportDirectory, since i have a preloaded folder in my app, which i want to add files to once the app have been initialized. I'm trying to NSLog the content of the file, so i can see if it actually worked. From the debugger, i can see that the content is , which i don't not what means. Anybody?

NSString *document = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", description, focus, level, equipment, waterDepth];

//NSLog(@"%@", document);

//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDirectory = [paths objectAtIndex:0];

//filename
NSString *filename = [NSString stringWithFormat:@"%@/%@", supportDirectory,[_nameTextField.text stringByAppendingString:@".txt"]];
NSLog(@"%@", supportDirectory);
NSLog(@"%@", filename);

[document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil];
NSLog(@"%@", content);
Niels Sønderbæk
  • 3,487
  • 4
  • 30
  • 43

2 Answers2

9

You're not passing a string encoding, you are passing NSStringEncodingConversionAllowLossy which is an option that can affect how encoding conversion happens for certain methods (not the one you're using). You need to pass something like NSUTF8StringEncoding.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • This is the real solution to the OPs problem, since he said he is experiencing the following error: Could not write file at path: Error Domain=NSCocoaErrorDomain Code=517 "The operation couldn‚Äôt be completed. (Cocoa error 517.)" UserInfo=0xd6dbae0 {NSFilePath=/var/mobile/Applications/80167553-DF41-49D6-BA1C-91D5EA6590FA/Librar‌​y/Application Support/test.txt, NSStringEncoding=1} – – Authman Apatira Sep 09 '12 at 19:22
  • I was also facing same issue, and its resolved by passing like NSUTF8StringEncoding. Thanks Ken – Sunil Targe Oct 07 '13 at 10:35
1

When you write or read files, it is highly recommended to use the error parameter and handle the cases where you get an error. Also, this is very useful to help you debug your code.

For example, in your case, you can do like this:

NSError *error = nil;
BOOL success = [document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error];

if (!success) {
    NSLog(@"Could not write file: %@", error);
} else {
    NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:&error];
    if (!content) {
        NSLog(@"Could not read file: %@", error);
    }
}

If you get an error that says that The operation couldn’t be completed. No such file or directory, that means that you did not previously create the folder. So you create it before trying to add content to it:

NSString *supportDirectory = [paths objectAtIndex:0];
NSError *error = nil;
BOOL success;
if (![[NSFileManager defaultManager] fileExistsAtPath: supportDirectory]) {
    success = [[NSFileManager defaultManager] createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error];
    if (!success) {
        NSLog(@"Could not create directory: %@", error);
    }
}
sch
  • 27,436
  • 3
  • 68
  • 83
  • In the debugger it says it could not write file at path? – Niels Sønderbæk Apr 27 '12 at 16:43
  • Could not write file at path: Error Domain=NSCocoaErrorDomain Code=517 "The operation couldn‚Äôt be completed. (Cocoa error 517.)" UserInfo=0xd6dbae0 {NSFilePath=/var/mobile/Applications/80167553-DF41-49D6-BA1C-91D5EA6590FA/Library/Application Support/test.txt, NSStringEncoding=1} – Niels Sønderbæk Apr 27 '12 at 16:48
  • I'm not quite sure yet, but I'm working on a way to just put the files into the document folder instead to see if that works. Thanks for your help though, i lead me in the right direction :) – Niels Sønderbæk Apr 27 '12 at 22:27
  • You can verify if this works as you did at the beginning: write a string into a file than read it again. – sch Apr 27 '12 at 22:29
  • yeah, and it keeps giving me that i doesn't write the file, so i'm trying to figure that out. – Niels Sønderbæk Apr 27 '12 at 22:29