What is other option available in swift instead of refactoring and renaming class or attribute name ?

When I was truing to rename my class in swift it will show error like below.


Xcode can only refactor C and Objective C code.


I just want to know that, in Objective C it allow us to rename class name, then why it not available in Swift, or any other way to do the same.
If there is any good way then please suggest me.
Thanks.

Accepted Reply

That's why I am asking for any short cut which is availabel in Objective C …

Everyone agrees that Xcode should support refactoring for Swift. Rest assured that it’s on the to-do list (r. 16436730). I can’t comment as to when it might happen, other than to say that it’s not in Xcode 8.

In the meantime, the suggestions posted by rsharp et al should help.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

As you've found, refactoring in Swift has not yet been implemented. You'll need to do things manually and there are generally two approaches:


1. If what you are renaming has a very unique name to start with, you can probably get away with search-and-replace.


2. If what you are renaming has a very general name, just edit that name, re-compile and address and the errors.

It might help when renaming to do an option-clean build folder to help Xcode to use the new name.

>> 1. If what you are renaming has a very unique name to start with, you can probably get away with search-and-replace.


>> 2. If what you are renaming has a very general name, just edit that name, re-compile and address and the errors.


In addition to these tips, I'd add:


— You can refactor a name everywhere within its scope of definition within a single file using the "edit in place" function, accessed by selecting some or all of it and typing Command-Control-E (in the standard keybindings, and there is of course a menu item for it somewhere). If it's a class name, this will refactor it throughout the file. If it's an instance variable, it will refactor it throughout the class within the file. However, clashes between the new name and any existing names won't be detected, so you'd want to search the file first, if there's any doubt.


This doesn't refactor globally, of course, but in many cases most of the occurrences are within a single file, and it's easy to fix the rest via compile errors.


— The global search-and-replace UI (in the search panel on the left, accessed via Command-Option-Shift-F) allows you to preview changes in a side-by-side comparison view, before committing the changes. In the comparison view, you can turn individual changes on or off, in case your search found some false positive matches. Also, the left pane of the view is editable, so if there are some related changes that need to be made at the same time, you can do that right there.


I don't use this global search pre-flighting much, but it's very useful for some kinds of changes.

>I don't use this global search pre-flighting much, but it's very useful for some kinds of changes.

➕ 1

Yes, all of you are right guys. But in search and replace case I can only change text within the specific file or you can say within all over project if it is just attribute, method or some other text. But what about if I want to change main class name, in that case I have to manually change the file name also and when trying to save that file it will ask for Save Anyway.

In short, I have to do that task manually, and it is irritating for developer. That's why I am asking for any short cut which is availabel in Objective C to Re-Factor class name, it will change file name also and itself in all over project except where it treated as a common string.

That's why I am asking for any short cut which is availabel in Objective C …

Everyone agrees that Xcode should support refactoring for Swift. Rest assured that it’s on the to-do list (r. 16436730). I can’t comment as to when it might happen, other than to say that it’s not in Xcode 8.

In the meantime, the suggestions posted by rsharp et al should help.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"