50

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.

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

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

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

Naresh
  • 16,698
  • 6
  • 112
  • 113
Rod
  • 1,975
  • 4
  • 19
  • 23
  • If the classes are so similar, it seems like the commonalities should be extracted into a superclass so that the subclasses contain only the unique behavior. – Chuck Jul 07 '09 at 19:22
  • 5
    This is a silly comment. The commonalities are the include files for the superclass, naming convention (In my case I have RemoteSystem, RemoteFolder, etc. inheriting from RemoteObject), init functions with the proper [super init...]. You know, the things a subclass needs to define. – Rob Osborne Sep 19 '10 at 19:19
  • 5
    Yeah. That was a silly comment. I need to be able to duplicate a file too. – Arcadian Sep 21 '11 at 13:35

7 Answers7

74

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

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!

WiseOldDuck
  • 3,156
  • 2
  • 24
  • 27
  • Watch out if you use source control. For some reason after duplicating the files they were not added to my source control. – Steve Moser Jan 23 '12 at 16:18
  • It's not under the File menu in Xcode 4.2. It's under the Edit menu and it is always disabled when a file is selected. – Alex Zavatone Apr 19 '12 at 20:22
  • @Alex there is, confusingly enough, Duplicate on both the File and Edit menus, doing different things. File --> Duplicate is the one you want, to duplicate a file, as asked about in the original post. It's right after Save. – WiseOldDuck Apr 19 '12 at 23:17
  • @Alex Well I'm on 4.3 now so can't easily verify but was using 4.2.x when I posted this - are you on the latest point version of 4.2? I'd love to figure out exactly when this was added and I'll update my answer. – WiseOldDuck Apr 21 '12 at 21:28
  • 1
    Yeah, I'm on 4.2.1. Posted Date: October 12, 2011, Build: 4C199. Some guys from the Xcode team mentioned that there would be no more updates for Xcode on Lion, but since I hate Lion with the burning passion of a thousand suns, I'm only going to move there when there are no other options. – Alex Zavatone Apr 21 '12 at 21:41
  • Wow! I never noticed File → Duplicate…! :-p – Constantino Tsarouhas Mar 27 '13 at 20:43
  • 1
    In XCode 5.1 when I duplicate files to create a new class, there's a bug that sometimes prevents the duplicated .m file from actually building. The linker was saying the class was missing even though I cleaned and it clearly should have been building. If instead I use File->New and create a new class and manually copy the text contents of the non-building duplicated file, then it all works fine. – Paul Slocum Apr 30 '14 at 00:42
18

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

shinto Joseph
  • 1,039
  • 3
  • 16
  • 27
  • targets for the new copy ain't right. Which is a problem if the file being duplicated is in two dozen targets. – Anton Tropashko Oct 31 '17 at 12:34
  • Xcode 15 (beta 2), I just used this command. It appears to take the current target list, and display that as the default target list for saving the new file... – benc Jul 21 '23 at 18:14
8

"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.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • what about xib duplication? – Anton Tropashko Oct 31 '17 at 12:35
  • @AntonTropashko That is a good point, this is a pretty old answer - although Xcode may not let you duplicate some things, you can use Finder to duplicate something like a xib file, then drag it bak into the project after changing the name and re-work. – Kendall Helmstetter Gelner Nov 22 '17 at 23:59
  • yeah, that is obvious, except xib is a member of about 30 targets. you see my problem? – Anton Tropashko Nov 23 '17 at 08:00
  • Be careful how you do this - I had some slight variation in my steps, and several dozen commits later realized the files were OUTSIDE my project folder, referenced with relative paths, and not actually committed to my repo. It was a disturbing discovery. – Erhannis May 08 '20 at 00:36
3

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:

Is not part of the build phase of any targets in the Xcode 3 projects in this workspace and so can't be refactored

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
Nike Kov
  • 12,630
  • 8
  • 75
  • 122
1

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.

David Maymudes
  • 5,664
  • 31
  • 34
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.

#!/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" );
}
Peter N Lewis
  • 17,664
  • 2
  • 43
  • 56
0

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.

Naresh
  • 16,698
  • 6
  • 112
  • 113