IOS: Moving file to directory

I have this code and error with text:

ERROR: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x145b5a60 {NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSUserStringVariant=( Move ), NSFilePath=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSDestinationFilePath=/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Documents/Files/temp.zip, NSUnderlyingError=0x1457af70 "The operation couldn’t be completed. No such file or directory"}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *pathDocumentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dictionaryName = @"Files";
NSString *filesPath = [[pathDocumentDirectory objectAtIndex:0] stringByAppendingPathComponent:dictionaryName];
NSString *toPath = [filesPath stringByAppendingPathComponent:@"temp.zip"];
NSString *fromPath = downloadedItem.contentURL.path;
NSError *error = nil;
BOOL isDir = YES;

if([fileManager fileExistsAtPath:fromPath])
{
    // Create PurchasedBooks directory
    if (![fileManager fileExistsAtPath:dictionaryName isDirectory:&isDir]) {
        [fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
    }
    [[NSFileManager defaultManager]moveItemAtPath:fromPath toPath:toPath error:&error];

    if (error) {
        NSLog(@"ERROR: %@", error.description);
    }
}

Really do not understand why this happens

Upvotes: 0

Views: 867

Answers (1)

Joe Smith
Joe Smith

Reputation: 1920

Add error checking for:

[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];

Upvotes: 0

Related Questions