How to move a directory to a new location on iPhone

Here is my method that should simply move the contents of the directory from / someDirectory to / addons / id / UUID:

  CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID
  //get the string representation of the UUID
  NSString *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);

  //MOVE the addon to the addons directory addons/shortname/UUID
  NSString *pathToAddon = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"%@", relPath]];
  NSString *pathToAddonDest = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"addons/%@/%@", [character objectForKey:@"shortName"], uuidString]];

  // move the files
  NSError* error;
  if([[NSFileManager defaultManager] moveItemAtPath:pathToAddon toPath:pathToAddonDest error:&error] != YES)
  {
    NSLog(@"Unable to move file: %@", [error localizedDescription]);
  }

  //release the uuid stuff
  [uuidString release];
  CFRelease(uuidObj);

The transition is not performed. The operation could not be completed. (Cocoa error 4.). However, the same code works if I change pathToAddonDest to:

NSString *pathToAddonDest = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"addons/%@", [character objectForKey:@"shortName"], uuidString]];

Therefore, I can write from / someDirectory to / addons / someDirectory, but not from / someDirectory to / addons / someDirectory / UUID.

Any ideas why a seemingly simple renaming wouldn't work this way?

+3
source share
1 answer

You must create a directory before moving it there. / addons / someDirectory / UUID --- create this path before trying to move the contents.

+3
source

Source: https://habr.com/ru/post/1787437/


All Articles