19

In my xcode project i have created a folder manually named "MyClasses" to place the newly added files/classes to this folder.

Now i have nearly 30 classes in this folder.

when i renamed this folder , all the files in this folder are gets Erased.

Now i need to rename the folder to "ViewControllerClasses".

But i lost 30 .h, .m, xib files. [lucky i have a copy & and zip file]

How to rename the folder with out corrupting the files.

enter image description here

enter image description here

Renamed it to myviewcontrollerclasswes

When i renamed

enter image description here enter image description here

Ranga
  • 821
  • 4
  • 11
  • 20

5 Answers5

27

Two Solutions

Method_1. Try Manual editing:

  « Drag your MedChart.xcodeproj to TextWrangler  (or any text editor) 

  « Use find button to find "FolderName" (Your folder name) 

  « Replace all with new name.

  « In some project you may notice header search path problem...goto header search path and replace with new name.

Method_2. Use Xcode to choose base folder:

Rename folder in finder then use Xcode to choose same folder. enter image description here

Guru
  • 21,652
  • 10
  • 63
  • 102
19

NOTE as of 2017: Now Xcode 9 synchronises the folder and the project hierarchy automatically.

NOTE as of 2013: Duplicate and rename Xcode project & associated folders


The "folders" what you see in XCode under your project file are not real folders in the file system, only virtual folders administered in the .xcodeproj file. You can organize your files in the project folder independently from their location in the file system. Some prefers to map the file system folder structure inside the project folder structure, some others store all source files in one big folder in the file system and organize them only in the project folders: it's rather a question of preference.

However if you rename/move physically the files in the file system, you will have to delete and re-add them to your project since XCode will not know where to find them. Pay attention not to delete them physically only remove them from XCode project, then re-add them and reorganize as you want.

One more thing to note: if you are using version control system you will have to inform also its client (svn or git most likely) that you have renamed/moved your files. If you want to keep file revision history it will be a good idea to issue the copy/move command explicitly to the version control otherwise it will treat your files as deleted from the old location and added as new in the new location.

MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • 3
    +1 for the hint about version control! Regarding delete and re-add of the group after physically renaming the folder in the file system: I'm not sure about other versions, but in Xcode 4.2 this is not necessary. You can select the group in Xcode, display the File Inspector (keyboard shortcut Option+Command+1), then in the File Inspector window click the small button below the "Path" combo box. This will pop up a file dialog where you can choose the folder that the group represents. – herzbube Jun 09 '12 at 17:43
  • Nice, I did not know about this. I was not completely exact in the answer above since it is also possible to create groups in project folder that maps directly a folder in the file system and their contents will be synchronized automatically. They have blue folder icon in XCode if I remember well. However they are mostly used for resources only. – MrTJ Jun 11 '12 at 06:56
  • You would have thought 4.6.2 versions in Apple will have sorted out this disgraceful system – Adam Waite Jun 04 '13 at 15:14
  • This is not any more 100% correct. Now xcode 9 can create folder with real folders in the file system. – Stephan Dec 26 '17 at 16:15
  • Thanks @Stephan, I added an updated to the old answer. – MrTJ Jan 02 '18 at 10:54
5

In case anyone is still having trouble with this:

  1. Select the folder or file in the left-side bar of XCode corresponding to the folder you want to rename (for me I renamed the folder containing all my files so I selected the topmost folder)
  2. On the right-side bar, below where it says 'Location', click the folder icon.
  3. In the file explorer that opens up rename the existing folder to whatever name you'd like it to be, then afterwards select it and press 'Ok'.
  4. XCode should update the file locations accordingly.

Keep in mind that if you rename something which contains a file that is hard-coded as a certain path in your build settings, XCode will throw an error. You'll have to manually change those paths in your build settings.

This worked for me, hope it works for you too.

Shady
  • 131
  • 1
  • 3
  • This is a much better answer than the accepted one. You do not have to remove and add all the files manually. Be sure to update the path to the info.plist however. – picciano Jun 23 '16 at 13:02
3

You should just run a search and replace on the project file (if you are working with version control and with other developers you will have seen this file a lot without a doubt), it's the project.pbxproj file located inside the xxxxxxxx.xcodeproj file.

Just right click (Control + Click) on the file and select "Show Package Contents" to find the xcodeproj file.

Daniel
  • 23,129
  • 12
  • 109
  • 154
  • 1
    Dan - fantastic awesome tip, thanks for that. I believe the file may actually be today "UserInterfaceState.xcuserstate" .. I edited in some long instructions for any future readers. thanks again man, merry xmas. – Fattie Dec 06 '13 at 09:29
1

I still find it easier than all these methods to simply create a new folder in your file navigator and then drag your files from the other folder into it. Sometimes the simplest way is the best way. It literally takes me 10 seconds and I don't need to leave Xcode.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85