Xcode can only refactor C and Objective-C code. How to rename swift class name in Xcode 6?
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
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.
Related videos on Youtube
Khawar
Updated on October 26, 2020Comments
-
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 errorXcode can only refactor C and Objective-C code
.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 almost 10 yearsThere is none currently. File a bug.
-
HughHughTeotl almost 9 yearsOver 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 over 8 yearsAppCode 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 about 7 yearsEven after two years!!! NOT IMPLEMENTED???
-
LegendLength about 7 yearsIt's actually nice in a way brings back memories of old times while copy and pasting 100 items in C
-
-
Khawar almost 10 yearsthanks 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 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 almost 10 yearsThanks 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 almost 10 yearsNo 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 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 over 8 yearsI'm on Version 7.2 (7C68) and the renaming refactoring is still not possible
-
ManiaChamp about 8 yearsIn 7.3 its also not possible.
-
hdsenevi over 7 yearsThis helps. I'm amazed that I missed this for so long!! :]
-
kisileno almost 7 yearsThank you, thank you Apple, but renaming still doesn't work. Now it is crashing the Xcode.
-
Guy Daher almost 7 yearsTrue... 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 almost 7 yearsThis only works within the scope, i.e. not across files IIRC.