26

I am trying to rename the class name in swift but it gives me error Can't refector swift code. I want to know the best way to rename a file that will reflect in the whole project

Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • 3
    global search & replace with cares – Bryan Chen Dec 02 '14 at 04:30
  • 1
    see this answer http://stackoverflow.com/questions/15839664/xcode-c-and-objective-c-refactoring – Kalenda Dec 02 '14 at 04:33
  • 1
    @moise2022: No, that question and its answers are for Objective-C and does not apply to Swift code. – Martin R Dec 02 '14 at 06:43
  • Instead of renaming class, how about to use typealias? – sooop Dec 02 '14 at 08:09
  • @MartinR Xcode is the IDE in witch he's editing his code. Of yet, Xcode only refactor C and Objective-C code. The solution given in the above mentioned link is an alternative which applies to any other language you may write/edit in Xcode, Swift included. – Kalenda Dec 02 '14 at 09:18
  • 1
    Possible duplicate of [Xcode can only refactor C and Objective-C code. How to rename swift class name in Xcode 6?](http://stackoverflow.com/questions/24133664/xcode-can-only-refactor-c-and-objective-c-code-how-to-rename-swift-class-name-i) – moumoute6919 Oct 19 '15 at 20:49

8 Answers8

31

Currently you can't refactor in Swift. However, you can do Find menu > Find and Replace in Project. (Carefully)

Edit: Xcode 9 allows you to refactor.

Highlight your property/function and choose Editor Menu -> Refactor -> Rename

Wait a second or two for Xcode 9 to scan your project for all occurrences and then type the new name.

Caroline
  • 4,875
  • 2
  • 31
  • 47
  • You'd have to rename it in the project.pbxproj file as well, right? – Ivan Lesko Oct 28 '15 at 18:08
  • @IvanLesko - no, if you rename it in all the source code that should be sufficient. – Caroline Oct 28 '15 at 22:35
  • @Caroline wow, still after three years can't rename in Swift :/ – JHollanti Aug 03 '17 at 08:23
  • @Caroline, nice! better late than never :) thanks for the heads up – JHollanti Aug 04 '17 at 12:47
  • In my case, I have to rename the file in Finder as well. Otherwise the class file cannot be found. – Protocole Sep 26 '17 at 04:11
  • @Protocole - if you're renaming a file, you can do that in the Xcode Project Navigator. This will rename the file on disk. Additionally, as of Xcode 9, if you create a group, this will create a directory on disk, further organising the files on disk. (If you right click to create the group you can choose not to create a new directory.) – Caroline Sep 27 '17 at 08:07
6

Now it's possible in Xcode 9.

Right click on class name and choose Rename.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
4

After doing what @Caroline has pointed out, you should also change the name of your .swift file because it does not change using Find > Find and replace.... For doing that, you can click on the name of a .swift class and rename it as you want.

enter image description here

I am using XCode 8 and it is the best option I could find to change the name of my class in the whole project.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
3

EDIT, The Latest Solution:

Starting Xcode 9 you can now do it easily. These are the steps:

  1. Highlight the code that you want to rename
  2. right click and choose refactor -> rename.

Or you can also use the Editor menu on top of the Xcode and choose refactor -> rename. Done!!

Old solution for XCode 9 and below:

  1. click Find -> Find and Replace in Workspace

enter image description here

  1. Rename it, for example I am renaming ChatViewController with ChatVC then hit Enter. This usually rename the naming in storyboard too, So it's really convinient, but just double check if you want to really make sure it's changed.

enter image description here

  1. Lastly, Dont forget to rename the file itself in the project navigator

enter image description here

Muhammad Fauzi Masykur
  • 1,994
  • 1
  • 15
  • 18
1

With Xcode 9, to be released in fall 2017 - Developers would be able to - 1. Refactor their Swift code. 2. Rename Swift project classes. 3. Run multiple instances of the simulator at once. 4. Run and debug on physical devices wirelessly. 5. Access, pull, push and commit to Git directly from XCode.

My personal favorite is the way issues are highlighted in red in multiple lines completing the sentence of the issue.

This WWDC 2017 session is a good view

XCode is getting smarter for us.

user550088
  • 712
  • 1
  • 8
  • 16
0
  1. Find & Replace by cmd + shift + F
  2. Rename class name.
  3. If you use storyboard/Nib file, don't forget change the class you assigned in Show the identify inspector to new class name.

TIP Set scope under the search field for Find & Replace can make this work clear.

William Hu
  • 15,423
  • 11
  • 100
  • 121
0

AppCode by Jetbrains can refactor/rename both Swift and Objective-C classes quite easily; It's not free but it does a great job.

Tahir Kizir
  • 83
  • 2
  • 10
ApriOri
  • 2,618
  • 29
  • 48
0

If you are facing similar error I was facing, where an error pops up when I choose the rename menu, just build the project and try again. It seems like the Xcode needs some kind of parsing table to refactor the class name.

Tkwon
  • 1