【问题标题】:How to move a directory to a new location on iPhone如何在 iPhone 上将目录移动到新位置
【发布时间】:2011-01-24 11:50:18
【问题描述】:

这是我的方法,它应该简单地将目录的内容从 /someDirectory 移动到 /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);

移动失败,操作无法完成。 (可可错误 4。)。但是,如果我将 pathToAddonDest 更改为:

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

所以我可以从 /someDirectory 写入 /addons/someDirectory 但不能从 /someDirectory 写入 /addons/someDirectory/UUID。

任何想法为什么看似简单的重命名不能以这种方式工作?

【问题讨论】:

    标签: iphone objective-c nsfilemanager


    【解决方案1】:

    您应该先创建目录,然后才能将其移动到那里。 /addons/someDirectory/UUID --- 在尝试移动内容之前创建此路径。

    【讨论】:

    • 几乎正确。您必须创建路径,直到要移动的实际文件名。所以 /addons/someDirectory 必须存在,但 /addons/someDirectory/UUID 必须不存在。感谢您的想法!
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多