Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my copy here?

I'm trying to copy a file from my application bundle to my app's documents directory. I'm getting an error, "Cocoa Error 262". What am I doing wrong? Here's my code:

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];
NSURL *initialURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]];

NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:[initialURL absoluteString]]) {
    NSLog(@"Original does not exist. \nPath: %@", [initialURL absoluteString]);
}  

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL absoluteString]]) {
    NSLog(@"Destination file does not exist. \nPath: %@", [storeURL absoluteString]);

    [[NSFileManager defaultManager] copyItemAtURL:initialURL toURL:storeURL error:&error];

    NSLog(@"Error: %@", [error description]);
}
like image 420
Moshe Avatar asked Aug 30 '11 01:08

Moshe


People also ask

Why is my copy not working?

Malware or viruses can corrupt or disable the clipboard. A large clipboard history that hasn't been cleared recently can cause problems. A faulty keyboard or mouse may not allow you to copy or paste correctly. Corrupt system files or drivers can interfere with the clipboard.

How do you fix you can't paste this here because the copy area?

Cause: The Copy area and the Paste area are not the same size and shape. Solution: Select the upper-left cell instead of the whole range before you paste. Click the cell where you want the upper-left cell of the copied data to appear. On the Home tab, click Paste.


2 Answers

The problem is you're initializing a URL with a plain old file path.

NSURL *initialURL = 
    [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" 
                                                         ofType:@"sqlite"]];

Use [NSURL fileURLWithPath:] instead.

like image 126
Darren Avatar answered Oct 22 '22 03:10

Darren


The error you are getting is

NSFileReadUnsupportedSchemeError Read error because the specified URL scheme is unsupported

which I believe would mean one of your paths is not forming correctly. perhaps write these paths to the log and see if they are coming out as you expect them to.

like image 44
smitec Avatar answered Oct 22 '22 02:10

smitec