How to rename the class name in swift? - ios

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

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.

Now it's possible in Xcode 9.
Right click on class name and choose Rename.

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.
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.

EDIT, The Latest Solution:
Starting Xcode 9 you can now do it easily. These are the steps:
Highlight the code that you want to rename
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:
click Find -> Find and Replace in Workspace
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.
Lastly, Dont forget to rename the file itself in the project navigator

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.

Find & Replace by cmd + shift + F
Rename class name.
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.

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

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.

Related

Is there any better way to rename an XCode project than the given one?

The given version is described here: How do I rename a project in Xcode 5?
But, as you can see from the following screenshot^, not everything gets renamed, and if one opens package contents, directory names etc. to try to manually change all the references, the project is broken afterwards, so you have to keep the old project name for sub directories etc., which granted is not a massive problem, but is intensely irritating, and I'm not enjoying being intensely irritated at work.
^ apologies for a screenshot, but there's too much information in it to transcribe to written text. Top two-thirds of screenshot is XCode project, bottom third a Finder window.
Renaming projects in xcode in one of the most annoying things in iOS development. I assume you want to rename your app. I faced this problem once and figured out a simple, clean way to do it.
Go back to the point where everything worked.
Open project in xcode and click on the project icon in the project structure( first file)
Go to the info tab
Search for Bundle Name. Most probably it will automatically be set to $(PRODUCT_NAME) which is a shell variable that will set your app name the same as the project name.
Set it to whatever you want your app name to be
Done
Notes :
If you use custom URL Schemes this might produce an error when redirecting.
ALWAYS git or some other SVN in your projects. This will come in handy in this kind of situations

How to add new class file to group in Xcode?

I have noticed that now whenever I do command-N to create a new Cocoa Touch class in my project, it keeps adding the file outside of the project in the file tree:
The behavior I remember (I haven't touched this project in a few weeks), is that whenever I create a new class it automatically places it in the group that you are currently working in, something like this:
Is there any way to restore this behavior? Creating a new plain .swift file seems to give you the choice of picking the group, but creating a new Cocoa Touch class does not. Regardless, I don't remember having to pick the group at all, it "just worked".
Please let me know if you need any more info. Xcode v6.3.2/6.4 on El Capitan
EDIT: I have tested further, and it seems the correct behavior is observed when adding new swift files and playgrounds, but not when adding new Cocoa Touch Classes.
EDIT 2: Here is photos of the file creation process and how it isn't working:
Step 1) Command-N, select new Cocoa Touch Class
Step 2) Name class like usual:
Step 3) Here is where the problem seems to lie. It gives me the option of placing where I want my files to be saved to on the disk, but it isn't allowing me to choose a group or target:
I also discovered that clicking the Options button on the bottom seems to bring up the group and target selection pane, but they are grayed out and cannot be interacted with:
EDIT 3: Just tested same action with Xcode 7 and Xcode 7 gets it right. Seems to be some kind of bug with Xcode 6.3.2/6.4
I had the same problem in Xcode 6.4.
Just create a new Cocoa Touch class in your project and after that, delete the reference to this file(delete->Remove Reference). After that go to File->Add File to your project, find your file in project folder. There you will be able to selecte target project in select the Group.
I think it will help you :)
Step 1: Command + N to create a new class
Step 2: Name your class
Step 3: Specify the group where you'd like the class to go

Core data: Unable to load class named "CDAccount"

I've recently downloaded iOS 8 to make sure that my existing app works properly with it, but I'm getting a warning in the debugger on launch:
"CoreData: warning: Unable to load class named 'CDAccount' for entity 'CDAccount'.
Class not found, using default NSManagedObject instead."
After some research, it seems like people have had similar issues when using Core Data in Swift, but all my code is in Objective-C (and the data model hasn't changed for at least 6 months).
Any advice is much appreciated, I'm really nervous that my app isn't going to function properly once people start upgrading to iOS 8!
I had the exact same problem in Objective-C and Xcode 6. For some reason, Xcode had removed my 'CustomNSManagedObject'.m classes from my Compile Sources.
Go to your project target -> Build Phases -> Compile Sources and use the + button to add CDAccount.m
If you are using Mogenerator to create a _CDAccount.m file, add that to your Compile Sources as well.
Remove the "dot" in Configurations "Default" on .xcdatamodeld:
to:
Rebuild the application.
My issue was also similar, but the reason was that module of entity was not set.
It seems to be an Xcode bug/issue. I'm using Xcode 6.3.1
click on your core data entity (xxx.xcdatamodeld), then select the entity, and in the attributes inspector you'll see that the entity's class name is set to:
PRODUCT_MODULE_NAME.xxx, where 'xxx' is your entity's name.
solution : You have to replace PRODUCT_MODULE_NAME with your app's name.
If in doubt about your app's name, check under 'build settings', filter on 'product_module_name', and you'll find your app name under 'packaging'
in some cases (my case) the data model file (swift or objective-c) is also removed by Xcode from the project - it's still on in disk though, but you need to add it again. So in this case take the 'CDAccount.m' file & drop it in your project again (or use another of the several ways to do this)
My issue was similar, but the listed solutions didn't work because my CD Classes were stored in a framework. The solution was to change the Class Name Representation to "frameworkName.className". However, the xcdatamodeld didn't allow me to change it to that, so I had to "Show Contents" on the file, open the "contents" file in XCode, and make the changes myself.

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

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.
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
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.
I've been using Find>Find & Replace in Project, it's not perfect but it works.
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
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.
Xcode 9
Xcode 9 now supports renaming in Swift. This was announced in WWDC 2017.
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.

After changing project name, cannot find version setting

Using XCode 4 for iOS. I changed the project name of my program to something more user friendly and readable. I can no longer find the version setting.
Is there another setting I need to change to view and edit version/build?
Thanks...Rick
I have tried in XCode and rename the project.
The Xcode will automatically ask you to rename the target etc.
Afterward, I could still find the version and build in Target summary and info.
I rename the project name in the following way :
1. Select Project (the top row with blue icon)
2. Press Enter
3. Rename
4. And continue with xCode suggestion for renaming other related files
I hope this help

Resources