iPhone: How to copy a file from resources to documents?
<p>I just started with iPhone development. In one of the example where I had to display some data stored in sqlite db in a table view in a tabbar controller, I had to move a sqlite file from application bundle to the documents folder.</p>

<p>I used the application template - iOS > Application > Window-based application for iPhone (Used core data for storage)</p>

<p>In the template generated by XCode (base sdk set to latest iOS = 4.2), the following code was there...</p>

<pre><code>- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
</code></pre>

<p>In trying to get a path to documents folder, I used the method given above like this...</p>

<pre><code>NSString *documentDirectory = [self applicationDocumentsDirectory];
</code></pre>

<p>This gave a warning - <code>warning: incompatible Objective-C types initializing 'struct NSURL *', expected 'struct NSString *'</code></p>

<p>So I changed the code to following...</p>

<pre><code>// Added the message absoluteString over here
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];

NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&amp;error];
if (!success) {
   NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
</code></pre>

<p>And BOOM!!! It gave the error that - <code>'Failed to create writable database file with message 'The operation couldn’t be completed. No such file or directory'.'</code></p>

<p>How should I find the path to documents directory since the method <code>applicationDocumentsDirectory</code> generated by XCode template does not work for me. </p>

<p>Also, can somebody throw some light on the purpose of <code>applicationDocumentsDirectory</code> method given above. </p>

<p>Thanks</p>
