How to add new class file to group in Xcode? - ios

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

Related

Can't make files or folders inside main Xcode project folder

I am trying to create swift files and groups inside my main Xcode project group but for some reason it doesn't let me do it. When selecting new file it creates it outside of the group selected, and if I try and create a new group inside, the option is just greyed out.
I have never had this issue before with Xcode 11 and earlier, is this a feature that has been removed? Do I now have to make a separate group and work outside the initial project group?
Hopefully the image below will help explain what I mean.
NOTE: I am running Xcode 12.2 on BigSur 11.0.1
Here is what happens when I try to drag and drop the folder into the products group.
I also created an iOS App titled "Products". Upon changing the name to something else (Product Manager) I was able to add files to the project
in the second step of creating the file, do you still have selected the right folder?
Or is there maybe an other folder selected?

Why the XCode 6 test Navigator does not use XCTest but SenTestingKit?

Any Test class I create from the Tests Navigator (the plus (+) button on the left bottom), it add:
#import <SenTestingKit/SenTestingKit.h> instead of XCTest.
This is my Xcode version: Version 6.1.1 (6A2008a)
Note: This is an existing project, old ones which I recently updated to use ARC instead of MRC, it is up and running and GHUnit (https://github.com/gh-unit/gh-unit) was working on it,
I've removed GHUnit in order to use XCTest.
These are the steps:
From the project navigator I click the + button to add a "New Test Class".
I set the Class name...
Set where the class will be saved...
The file is created, however instead of import XCTest it does import SenTestKit =(
However it creates properly if I create the class using: File > New > File then select "Test Case Class" click "Next" set the class name, the screen is different,
it show a "Subclass of" dropdown and XCTestCase is selected on it:
Then the XCTest class is created properly:
This what I did trying to solve the issue:
Did check if the test bundle "Wrapper Extension" was set to xctest (yeah it is)
Reinstalled my Xcode
Took the opportunity to upgrade to Yosemite =D
Did not solve the issue.
This happens even if I create a new project :(
Any thoughts?
I did contact Apple and looks likes this is a bug on XCode.
You can see the thread here: https://devforums.apple.com/message/1094651#1094651
Also, these steps can be used as an workaround:
File > New > File then select "Test Case Class" click "Next" set the class name.
The Test Class will be created properly.
I will post more details here once the Apple people update the bug I did report.

How to rename the class name in swift?

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.

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.

Resources