如何在XCode中复制文件?

时间:2021-01-19 14:11:14

Anyone know a good solution?

有人知道一个好的解决方案吗?

So far I have not found a better way than using File>New file and then copying contents from old file to new.

到目前为止,我还没有找到比使用文件>新文件然后将内容从旧文件复制到新文件更好的方法。

You can probably duplicate in Finder and re-import but that's almost same amount of work: switching to finder, duplicate, import new files.

您可以在Finder中复制并重新导入,但这几乎是相同的工作量:切换到Finder,重复,导入新文件。

Doing this with one class is not so hard, but what to do if you need to generate 10+ similar Classes based on superclass.

使用一个类来实现这一点并不困难,但是如果需要基于超类生成10多个类似的类,该怎么做呢?

In Eclipse you select file and then copy/paste it in same folder. In finder there's Duplicate.

在Eclipse中,您选择file,然后将其复制/粘贴到相同的文件夹中。在finder中重复。

There's a menu Edit > Duplicate. But it's ALWAYS disabled. I tried selecting various files, classes, methods. It's still disabled.

有一个菜单编辑>副本。但它总是禁用。我尝试选择各种文件、类和方法。它仍然是禁用的。

6 个解决方案

#1


5  

"Duplicate" is enabled for targets in XCode (pretty much nothing else that I know of).

XCode中为目标启用了“Duplicate”(我几乎不知道还有什么)。

If you have a substantial number of subclasses with the same starting point to replicate, why not make a class template from it? Then you can just use file->New to make new instances. It's fairly quick to do.

如果您有大量的子类,它们的起点相同,那么为什么不从中创建一个类模板呢?然后您可以使用file->New来创建新的实例。这是相当快的。

This is probably the simplest example:

这可能是最简单的例子:

http://www.macresearch.org/custom_xcode_templates

http://www.macresearch.org/custom_xcode_templates

Otherwise, I'd simply duplicate the files in Finder as many times as you need, name them, and drag them into XCode en-masse.

否则,我将简单地复制查找程序中的文件,按需要多次,命名它们,并将它们全部拖放到XCode中。

#2


53  

In XCode 4.2 (I know this is an old question) there is Duplicate under the File menu.

在XCode 4.2(我知道这是一个老问题)中,文件菜单下有副本。

Select the file (you can select multiple files but it doesn't appear to do anything useful) in the Project Navigator and then File->Duplicate. Hooray!

选择项目导航器中的文件(您可以选择多个文件,但它似乎没有任何用处),然后选择file ->Duplicate文件。万岁!

#3


7  

In Xcode 4.5 we can duplicate using File-> Duplicate or cmd + shift + S

在Xcode 4.5中,我们可以使用File-> duplicate或cmd + shift + S进行复制

#4


1  

You could use "Save As..."; you'd still have to go back and re-add the original files to the project, though.

您可以使用“Save As…”;不过,您仍然需要返回并重新添加原始文件到项目中。

It wouldn't be such a bad way to do a bunch of related classes, though: edit file, Save As "class2", edit file, Save As "class3", etc., then "Add Existing Files" and re-add all of the files but the last to your project.

做一些相关的类并不是一个坏的方法,但是:编辑文件,保存为“class2”,编辑文件,保存为“class3”等等,然后“添加现有的文件”,重新添加所有的文件,但是最后一个要添加到项目中。

#5


1  

I use the following perl script to duplicate a file pair in the Terminal. You give it the base name of the original and new file, and it copies the header and implementation (c/cpp/m/mm) file, then replaces all occurances of the base name with the new name, then adds them to subversion. You still have to add the new files in to Xcode and adjust the creation date in the comment (I've got a Keyboard Maestro macro for that), but its quicker than doing a lot of the steps manually. I operate with a Terminal window and four tabs pre-set to the Project, Source, Resources, and English.lproj directory which gives quick access for a lot of operations.

我使用以下perl脚本在终端中复制文件对。您为它提供原始和新文件的基本名称,然后它复制头文件和实现(c/cpp/m/mm)文件,然后用新名称替换所有发生的基本名称,然后将它们添加到subversion中。您仍然需要将新文件添加到Xcode中,并在注释中调整创建日期(我有一个键盘迈斯卓宏用于此),但这比手动执行许多步骤要快。我使用一个终端窗口和四个选项卡,预先设置为项目、源、资源和英语。lproj目录,为许多操作提供快速访问。

#!/usr/bin/perl

use lib "$ENV{HOME}/perl";
use warnings;
use strict;

our $cp = '/bin/cp';
our $svn = '/usr/bin/svn';
our $perl = '/usr/bin/perl';

our $source = shift;
our $add = 1;
if ( $source =~ m!^-! ) {
    if ( $source eq '-a' || $source eq '--add' ) {
        $add = 1;
        $source = shift;
    } elsif ( $source eq '-A' || $source eq '--noadd' ) {
        $add = undef;
        $source = shift;
    } else {
        die "Bad arg $source";
    }
}
our $dest = shift;

die "Bad source $source" unless $source =~ m!^(.*/)?[A-Za-z0-9]+$!;
die "Bad dest $dest" unless $dest =~ m!^(.*/)?[A-Za-z0-9]+$!;
my $cpp;
$cpp = 'c' if ( -e "$source.c" );
$cpp = 'cpp' if ( -e "$source.cpp" );
$cpp = 'mm' if ( -e "$source.mm" );
$cpp = 'm' if ( -e "$source.m" );
die "Missing source $source" unless -e "$source.h" && -e "$source.$cpp";
die "Existing dest $dest" if -e "$dest.h" && -e "$dest.$cpp";

our $sourcename = $source; $sourcename =~ s!.*/!!;
our $destname = $dest; $destname =~ s!.*/!!;

print "cp $source.h $dest.h\n";
system( $cp, "$source.h", "$dest.h" );
print "s/$sourcename/$destname in $dest.h\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.h" );

print "cp $source.$cpp $dest.$cpp\n";
system( $cp, "$source.$cpp", "$dest.$cpp" );
print "s/$sourcename/$destname in $dest.$cpp\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.$cpp" );

if ( $add ) {
    print "svn add $dest.$cpp $dest.h\n";
    system( $svn, 'add', "$dest.$cpp", "$dest.h" );
}

#6


0  

Careful! When you use duplicate ( CMD + Shift + S ) - Xcode have a problem with indexing headers. Also when u want to make a refactoring it can be next error window:

小心!当你使用duplicate (CMD + Shift + S)时——Xcode有索引头的问题。当你想重构时,它可能是下一个错误窗口:

如何在XCode中复制文件?

So there a couple of ways what to do, to fix that.

有几种方法可以解决这个问题。

  1. Delete derived data from menu Window > Projects. Restart Xcode.
  2. 从菜单窗口>项目中删除派生数据。重启Xcode。
  3. Product > Clean
  4. 产品>清洁

#1


5  

"Duplicate" is enabled for targets in XCode (pretty much nothing else that I know of).

XCode中为目标启用了“Duplicate”(我几乎不知道还有什么)。

If you have a substantial number of subclasses with the same starting point to replicate, why not make a class template from it? Then you can just use file->New to make new instances. It's fairly quick to do.

如果您有大量的子类,它们的起点相同,那么为什么不从中创建一个类模板呢?然后您可以使用file->New来创建新的实例。这是相当快的。

This is probably the simplest example:

这可能是最简单的例子:

http://www.macresearch.org/custom_xcode_templates

http://www.macresearch.org/custom_xcode_templates

Otherwise, I'd simply duplicate the files in Finder as many times as you need, name them, and drag them into XCode en-masse.

否则,我将简单地复制查找程序中的文件,按需要多次,命名它们,并将它们全部拖放到XCode中。

#2


53  

In XCode 4.2 (I know this is an old question) there is Duplicate under the File menu.

在XCode 4.2(我知道这是一个老问题)中,文件菜单下有副本。

Select the file (you can select multiple files but it doesn't appear to do anything useful) in the Project Navigator and then File->Duplicate. Hooray!

选择项目导航器中的文件(您可以选择多个文件,但它似乎没有任何用处),然后选择file ->Duplicate文件。万岁!

#3


7  

In Xcode 4.5 we can duplicate using File-> Duplicate or cmd + shift + S

在Xcode 4.5中,我们可以使用File-> duplicate或cmd + shift + S进行复制

#4


1  

You could use "Save As..."; you'd still have to go back and re-add the original files to the project, though.

您可以使用“Save As…”;不过,您仍然需要返回并重新添加原始文件到项目中。

It wouldn't be such a bad way to do a bunch of related classes, though: edit file, Save As "class2", edit file, Save As "class3", etc., then "Add Existing Files" and re-add all of the files but the last to your project.

做一些相关的类并不是一个坏的方法,但是:编辑文件,保存为“class2”,编辑文件,保存为“class3”等等,然后“添加现有的文件”,重新添加所有的文件,但是最后一个要添加到项目中。

#5


1  

I use the following perl script to duplicate a file pair in the Terminal. You give it the base name of the original and new file, and it copies the header and implementation (c/cpp/m/mm) file, then replaces all occurances of the base name with the new name, then adds them to subversion. You still have to add the new files in to Xcode and adjust the creation date in the comment (I've got a Keyboard Maestro macro for that), but its quicker than doing a lot of the steps manually. I operate with a Terminal window and four tabs pre-set to the Project, Source, Resources, and English.lproj directory which gives quick access for a lot of operations.

我使用以下perl脚本在终端中复制文件对。您为它提供原始和新文件的基本名称,然后它复制头文件和实现(c/cpp/m/mm)文件,然后用新名称替换所有发生的基本名称,然后将它们添加到subversion中。您仍然需要将新文件添加到Xcode中,并在注释中调整创建日期(我有一个键盘迈斯卓宏用于此),但这比手动执行许多步骤要快。我使用一个终端窗口和四个选项卡,预先设置为项目、源、资源和英语。lproj目录,为许多操作提供快速访问。

#!/usr/bin/perl

use lib "$ENV{HOME}/perl";
use warnings;
use strict;

our $cp = '/bin/cp';
our $svn = '/usr/bin/svn';
our $perl = '/usr/bin/perl';

our $source = shift;
our $add = 1;
if ( $source =~ m!^-! ) {
    if ( $source eq '-a' || $source eq '--add' ) {
        $add = 1;
        $source = shift;
    } elsif ( $source eq '-A' || $source eq '--noadd' ) {
        $add = undef;
        $source = shift;
    } else {
        die "Bad arg $source";
    }
}
our $dest = shift;

die "Bad source $source" unless $source =~ m!^(.*/)?[A-Za-z0-9]+$!;
die "Bad dest $dest" unless $dest =~ m!^(.*/)?[A-Za-z0-9]+$!;
my $cpp;
$cpp = 'c' if ( -e "$source.c" );
$cpp = 'cpp' if ( -e "$source.cpp" );
$cpp = 'mm' if ( -e "$source.mm" );
$cpp = 'm' if ( -e "$source.m" );
die "Missing source $source" unless -e "$source.h" && -e "$source.$cpp";
die "Existing dest $dest" if -e "$dest.h" && -e "$dest.$cpp";

our $sourcename = $source; $sourcename =~ s!.*/!!;
our $destname = $dest; $destname =~ s!.*/!!;

print "cp $source.h $dest.h\n";
system( $cp, "$source.h", "$dest.h" );
print "s/$sourcename/$destname in $dest.h\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.h" );

print "cp $source.$cpp $dest.$cpp\n";
system( $cp, "$source.$cpp", "$dest.$cpp" );
print "s/$sourcename/$destname in $dest.$cpp\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.$cpp" );

if ( $add ) {
    print "svn add $dest.$cpp $dest.h\n";
    system( $svn, 'add', "$dest.$cpp", "$dest.h" );
}

#6


0  

Careful! When you use duplicate ( CMD + Shift + S ) - Xcode have a problem with indexing headers. Also when u want to make a refactoring it can be next error window:

小心!当你使用duplicate (CMD + Shift + S)时——Xcode有索引头的问题。当你想重构时,它可能是下一个错误窗口:

如何在XCode中复制文件?

So there a couple of ways what to do, to fix that.

有几种方法可以解决这个问题。

  1. Delete derived data from menu Window > Projects. Restart Xcode.
  2. 从菜单窗口>项目中删除派生数据。重启Xcode。
  3. Product > Clean
  4. 产品>清洁