I want to write some values to an external file and use NSDictionary for this purpose:
NSString *s = @"123456";
NSString *key = @"key";
NSString *file = pathInAppDirectory(@"values");
NSDictionary *dic = [NSDictionary dictionaryWithObject:s forKey:key];
[dic writeToFile:file atomically:YES];
Ok, pathInAppDirectory looks like this:
NSString *pathInAppDirectory(NSString *fileName)
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *appDir = [documentDirectories objectAtIndex:0];
return [appDir stringByAppendingPathComponent:fileName];
}
What happens is exactly nothing: If I want to have look in the file somewhat later and want to display the values, null is returned. And a look into the folder of the iPhone simulator shows an empty folder, too. What happens there or, to be more specific: what does not happen?
You need to use
NSDocumentDirectoryinstead ofNSApplicationDirectory. It is not possible to write in the Application bundle.Cleaned up example: