Best way to copy or move files with Objective-C?

前端 未结 3 1822
有刺的猬
有刺的猬 2020-12-23 17:03

I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another?

  • is there some NSSomething method I c
相关标签:
3条回答
  • 2020-12-23 17:34

    NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you'd use NSFileManager since its easier to work with:

    if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] )
        [[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];
    

    However, NSWorkspace can easily move files to the Trash, which NSFileManager can't do.

    [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:foldername destination:@"" files:filenamesArray tag:&tag];
    

    Check the documentation for a more complete description of the two classes. (NSFileManager, NSWorkspace)

    0 讨论(0)
  • 2020-12-23 17:41

    [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]

    Here's the link to the class reference:

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/copyItemAtPath:toPath:error:

    0 讨论(0)
  • 2020-12-23 17:46

    You can also use FSCopyObjectAsync function. You can display file copy progress and you can also cancel file copy using FSCopyObjectAsync().
    Take a look at this post.

    FSCopyObjectAsync is Deprecated in OS X v10.8

    copyfile(3) is alternative for FSCopyObjectAsync. Here is example of copyfile(3) with Progress Callback.

    0 讨论(0)
提交回复
热议问题