1

When i try to write to file with this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"txt"];
NSError *error = nil;
NSString*s = @"some text";
BOOL successful = [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
NSLog(@"%d",successful);

Nothing happens. NSLog says:

(null)
1
  • That sounds like there was no error and it was successful. What does "nothing happens" mean? – Kal Apr 24 '13 at 19:59
  • @iOSGuru248 I can't be certain but it sounds an awful lot like he was expecting the "some text" string to be written to the path he specified or an error if it wasn't successful. I think this should be filed as a bug with Apple, given the main bundle isn't writable but no "access denied" error occurred. The system is apparently lying. – Joshua Nozzi Apr 24 '13 at 23:20

1 Answers1

2

That's because you're writing over the main bundle try this:

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];

Here we're trying to write over our app Documents directory. Because you can't write on the main bundle

Gerard
  • 360
  • 3
  • 9