104

I have created a new Single View Application Project in Xcode 6 beta version. I want to rename swift class from ViewController.swift to some other name. But when I select Refactor -> Rename, it gives error Xcode can only refactor C and Objective-C code.

enter image description here

Any idea how to rename swift class in Xcode 6?

UPDATE:

Finally Xcode 9 is supporting Refactoring for Swift. It took Apple 3 years to add this basic feature. Refactoring

Khawar
  • 8,881
  • 9
  • 41
  • 65
  • 9
    There is none currently. [File a bug](http://bugreport.apple.com). – jtbandes Jun 10 '14 at 05:44
  • 19
    Over a year after this was asked, the only answer is still a manual find and replace! Unbelievably backwards of Apple, this is a basic and essential feature for an IDE – HughHughTeotl Jul 09 '15 at 17:00
  • 1
    AppCode can accomplish this refactoring. It will be a two-step process for your class defined in a `.swift` file. 1. Select the class name and perform **Refactor > Rename**. 2. Select the `.swift` file and perform **Refactor > Rename**. – Daniel Zhang Oct 15 '15 at 02:41
  • 5
    Even after two years!!! NOT IMPLEMENTED??? – Milad Faridnia Feb 13 '17 at 07:45
  • It's actually nice in a way brings back memories of old times while copy and pasting 100 items in C – LegendLength Apr 06 '17 at 17:09

6 Answers6

25

You can change name of a class in File Inspector at the right side of Xcode6.

  • open your class
  • go to File Inspector > Identify and type section
  • rename existing class in "name" field.

That's all! Previously I manually rename the class name in the class file.

Newone
  • 291
  • 2
  • 2
  • 11
    Thanks for reply, but it only changes the class name only in file structure. I still have to change all instances of my class name everywhere in code. Whereas using Refactor->Rename... used to change name in file structure as well as in code. – Khawar Jun 10 '14 at 11:07
  • No problem! I just thought its a kind of a solution until Apple do something with this issue. You can use the Find and Replace function as well until that day. Any case, built in Refactor->Rename almost does the same actions. – Newone Jun 10 '14 at 21:46
  • 1
    @Newone It doesn't do the same thing. For instance, Find and Replace will search inside of strings, and Refactor->Rename should not do that. – Martin Epsz Apr 30 '15 at 14:54
15

I've been using Find>Find & Replace in Project, it's not perfect but it works.

Pescolly
  • 852
  • 11
  • 17
12

I use this way:

  • You can positioning the cursor on the variable that you want rename
  • Select Editor - Edit all in Scope (^⌘E)
  • Rename the variable and automatically all variable occurrences is edit
Alessandro Pirovano
  • 2,337
  • 24
  • 21
10

From this tutorial.

Split up your iOS8 Swift Code

First things first, let’s rename our View Controller to be something more meaningful. Open up your ViewController.swift file and replace all references to ‘ViewController’ with our new name, ‘SearchResultsViewController’. Rename the file as well to SearchResultsViewController.swift.

If you try to run the app from here you’ll get a crash. This is because our storyboard file hasn’t been updated to know about the new class! So open up the Main.storyboard, select your ‘View Controller’ object in the scene (the left-hand side nav), and then select the Identity Inspector (right-hand side, third button.)

From here let’s change the class from ‘ViewController’ to ‘SearchResultsViewController’. Now we should be back on track. Check that the project still works, and let’s proceed.

Community
  • 1
  • 1
iPatel
  • 41,165
  • 13
  • 109
  • 131
  • thanks for reply. I have already experimented this method. But it doesn't seems to be good way. If I am using ViewController.swift objects in 10 classes, I have to manually rename them in all classes. So much painful and waste for time :( – Khawar Jun 10 '14 at 06:05
  • @Khawar- Yes you are right ,, but might be there is not another option now and also swift is in beta so. don't worry about it .. hope apple will find another more easy way then (objective c ???) :) – iPatel Jun 10 '14 at 06:08
  • 1
    I'm on Version 7.2 (7C68) and the renaming refactoring is still not possible – Barbara R Jan 19 '16 at 10:35
  • In 7.3 its also not possible. – ManiaChamp Mar 30 '16 at 12:17
3

Xcode 9

Xcode 9 now supports renaming in Swift. This was announced in WWDC 2017.

enter image description here

Community
  • 1
  • 1
Guy Daher
  • 5,314
  • 5
  • 36
  • 64
  • Thank you, thank you Apple, but renaming still doesn't work. Now it is crashing the Xcode. – kisileno Jun 10 '17 at 20:47
  • True... As of Version 9.0 beta, renaming makes Xcode crash. But this is what's supposed to work in the _future_, whenever that may be :) – Guy Daher Jun 12 '17 at 13:26
2

Unfortunately, Apple hasn't gotten around to implementing it yet.

See http://www.quora.com/Why-isnt-there-a-refactoring-feature-for-Swift-language-in-Xcode-6 for more information.

vy32
  • 24,271
  • 30
  • 100
  • 197