Xcode can only refactor C and Objective-C code. How to rename swift class name in Xcode 6?

23,215

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

Xcode 9

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

enter image description here

Share:
23,215

Related videos on Youtube

Khawar
Author by

Khawar

Updated on October 26, 2020

Comments

  • Khawar
    Khawar over 3 years

    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

    • csiu
      csiu almost 10 years
      There is none currently. File a bug.
    • HughHughTeotl
      HughHughTeotl almost 9 years
      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
    • Daniel Zhang
      Daniel Zhang over 8 years
      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.
    • Milad Faridnia
      Milad Faridnia about 7 years
      Even after two years!!! NOT IMPLEMENTED???
    • LegendLength
      LegendLength about 7 years
      It's actually nice in a way brings back memories of old times while copy and pasting 100 items in C
  • Khawar
    Khawar almost 10 years
    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 :(
  • iPatel
    iPatel almost 10 years
    @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 ???) :)
  • Khawar
    Khawar almost 10 years
    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.
  • Newone
    Newone almost 10 years
    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.
  • Martin Epsz
    Martin Epsz about 9 years
    @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.
  • Barbara R
    Barbara R over 8 years
    I'm on Version 7.2 (7C68) and the renaming refactoring is still not possible
  • ManiaChamp
    ManiaChamp about 8 years
    In 7.3 its also not possible.
  • hdsenevi
    hdsenevi over 7 years
    This helps. I'm amazed that I missed this for so long!! :]
  • kisileno
    kisileno almost 7 years
    Thank you, thank you Apple, but renaming still doesn't work. Now it is crashing the Xcode.
  • Guy Daher
    Guy Daher almost 7 years
    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 :)
  • Steve Moser
    Steve Moser almost 7 years
    This only works within the scope, i.e. not across files IIRC.