用Objective-C复制或移动文件的最佳方法是什么? [英] Best way to copy or move files with Objective-C?

查看:152
本文介绍了用Objective-C复制或移动文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否有最佳做法来编写一个OSX程序,将文件从一个地方复制或移动到另一个地方?


  • 是否有一些NSSomething方法可以调用?

  • 是否必须使用Input /输出流?

  • 或者是最好的方法,也许只是依靠传递命令给finder?奖金的问题:我怎样才能得到百分之一的副本12%完成与这些方法之一?

    感谢您的帮助!

    解决方案

    NSFileManager和NSWorkspace都有移动,复制和删除文件的方法。通常你会使用NSFileManager,因为它更容易处理:

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

    但是,NSWorkspace可以轻松地将文件移动到垃圾箱,NSFileManager无法做到这一点。

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

    检查文档以获得对这两个类的更完整的描述。 ( NSFileManager NSWorkspace


    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 can call?
    • Do I have to work with Input/Output streams?
    • Or is the best way maybe to just rely on passing commands to the finder?

    Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods?

    Thanks for your help!

    解决方案

    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)

    这篇关于用Objective-C复制或移动文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆