How do you duplicate a file in XCode?

前端 未结 7 1191
名媛妹妹
名媛妹妹 2021-02-05 00:18

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 dupli

相关标签:
7条回答
  • 2021-02-05 00:43

    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:

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

    1. Delete derived data from menu Window > Projects. Restart Xcode.
    2. Product > Clean
    0 讨论(0)
  • 2021-02-05 00:45

    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.

    #!/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" );
    }
    
    0 讨论(0)
  • 2021-02-05 00:50

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

    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.

    0 讨论(0)
  • 2021-02-05 00:54

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

    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.

    This is probably the simplest example:

    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.

    0 讨论(0)
  • 2021-02-05 00:54

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

    0 讨论(0)
  • 2021-02-05 00:58

    In my case, one of my folder changed from one place to another place.

    I have "Home" folder in Controller folder, but unfortunately it's moved from Controller folder to Manager folder.

    I checked many times everything fine, but I'm getting Command PrecompileSwiftBridgingHeader failed with a nonzero exit code

    But after 2 hours i realised, my folder structure changed.

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