Renaming a class in retrospect? - xcode

To be on the safe side, and as my project is relatively small, when I wanted to change the name of a class in Swift, I changed it manually and went through the files individually - storyboard, class file, and in project navigator.
In Xcode, is there another way to do this to be sure of replacing all instances, or is manually the only way to go? Can you safely use a find and replace for this purpose?

Select the class name in the declaration, right-click/control click on it, choose "refactor" and select "rename". That will do a complete rename, including references to the class from your storyboards. It's much safer than doing it manually (assuming the Swift editor is as smart as the Objective-C editor - I'm still pretty new to Swift development, so I haven't tested this feature with Swift.)
(As of version 6.2, Xcode is only capable of doing this with Objective-C and C code.)

Related

My storyboard custom class property doesn't generate a custom class in Xamarin anymore?

I'm using Visual Studio for Mac and design my UI in a storyboard using Xcode interface builder.
I'm trying to create a custom class of a UILabel and usually I just select the control and add my custom class name in the class property in xcode, and when xcode synchronises with Xamarin, the custom class is generated as a new file in the xamarin project, but this has stopped working.
Actually is seems to have stopped syncing all together. Are there any logs that I can check to see why? I have restarted xcode. Visual Studio for Mac, cleaned the project, deleted the bin and obj folders from the active project and rebooted the machine without success.
Any tips is very much appreciated!
My answer is a workaround and not a true answer as this has never worked for me. I have always used Xamarin iOS Designer to create custom class, then after file generation, close xib/storyboard and reopen in Xcode Interface Builder. Double checking for [Register("ClassName"]` tag.
Select Label in iOS Designer:
View -> Pads -> Properties Add Class name in Class Field:
Should see custom class autogenerated in SolutionExplorer:
Ensure .designer.cs file has Register Tag:
Now you can close the .xib in VS for Mac and reopen in Xcode. It probably won't autocomplete. But any Outlets you create will be an instance of your custom class as well as any custom attributes specified in .cs files will apply at runtime. Not as convenient as creating the custom class with Xcode but at least now you can use your custom class with Xcode Interface Builder.
I love my XCode as well! Instead of doing what you used to do, you can
Open the file in Xcode interface builder as usual with the Label
Find and Open the ABCViewController.h file in the other XCode window.
Hold control on your keyboard, click on your label on the GUI and drag it to the ABCViewController.h file and it should prompt you to create an outlet and name the class.
Save and Quit, and you should see the new Properties in your ABCViewController.designer.cs file
There's definitely a new issue after the update as you can see here as well

Objective-C, Transitioning from .nib to .storyboard

I'm trying to update my application from using .nibs to the more recent .storyboard format on OS X. I'm not exactly sure what the best method of accomplishing this would be.
Currently I have both the old MainMenu.nib and Main.Storyboard in my project. It's easy enough for me to manually copy items from the .nib to the .storyboard, although how do I tell XCode to start using my .storyboard instead of the .nib? Is it possible to use both in the same project? Any suggestions welcomed.
although how do I tell XCode to start using my .storyboard instead of the .nib?
If you no longer want to use MainMenu.nib at all, then you set the NSMainStoryboardFile key in your Info.plist (and delete the NSMainNibFile key). That way, the storyboard file is used at launch.
Info.plist keys are documented here:
https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html

Unknown Window class Xcode 4.4

I recently organized the project folder for my Mac app, which contains over 40 custom classes. I organized these classes by putting them in folders.
After making Xcode aware of the new file locations, I ran my app. The following was logged...
Unknown Window class TSDirectoryWindow in Interface Builder file,
creating generic Window instead
I went into the compile sources, removed every file, then added them back (thinking maybe the compiler was still looking at the old locations). No luck.
I'm semi-new to Mac/Objective-C development, and I'm starting to think organizing my files was a bad idea. For all I know, this could be a big no-no. It appears to be a issue with every nib, as I get the same message when MainMenu is the main interface,
Unknown class 'TSAppDelegate', using 'NSObject' instead. Encountered in Interface Builder file at path
/Users/tsilva/Library/Developer/Xcode/DerivedData/Podsnatcher-dlctvrrkdaksfdbblvopdarwlslr/Build/Products/Debug/Podsnatcher.app/Contents/Resources/en.lproj/MainMenu.nib.
Any ideas on how I can get this fixed? Thank you for your time!
I was adding the files to the linker for the "MyAppTests" target, not the application itself. Whoops.

Monodevelop migrate 2.8 didn't add .h files

I just upgrade my MonoDevelop to the latest version (2.8). I also installed XCode4, since it's fully supported now. When opening my solution (which consists of several projects) I was asked to migrate my solution. I did this and saved everything.
However, I think something is missing. When I open the .xib file, the project on the right doesn't show .h files, like shown in the tutorial.
It also shows warnings that the outlets were not found in XCode.
When I add a new screen, it creates the .h file as it should and shows this in XCode. However all existing screens don't have these files, which makes it impossible to add/change outlets.
How can I fix this.
Check that your classes you expect to see in Xcode have explicit [Register("SomeName")] attributes, which register them with a name usable from Xcode/Obj-C/IB.
I would expect an imported project to have those attributes already, since they're required for xibs to be able to load the classes by name, but you could maybe have got away without those attributes by using outlets on "File's Owner".

A way to automatically organize #imports in Xcode

I love the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or ActionScript).
Is there a command in Xcode to update the #import directives at the top of.m Objective-C files based on the classes referenced within the file?
You can do this by creating an Automator action and use that in Xcode as well as everywhere in Mac OS X. To do that, do the following:
Start Automator -> New
Choose "Quick Action" (or "Service" on older MacOS/Automator versions)
add a "run shell script" action
use sort | uniq as the script and check the "output replaces selected text" checkbox
save and give it a name (e.g. "sort & unique")
check "Output replaces selected text"
After you saved it, you can just select your imports in Xcode, right click and choose your "sort & unique" action to organize your imports.
This is not as good as the organize import actions in Eclipse or IntelliJ, because it doesn't removes unused stuff etc. but it's better than nothing.
PS: Got that from WWDC 2012 Sessions - Session 402 Working Effeciently with Xcode (starting at 6:15)
EDIT
I started using AppCode, the Objective-C IDE from JetBrains, and it has features like "organise imports". You should check it out: http://www.jetbrains.com/objc/.
I'm resolving this as not currently a feature of Xcode :(
I've found Cedar Shortcuts to at least be usable. It's not as good as Eclipse's import feature (it doesn't organize imports), but it can keep you from having to go to the top of a class file and typing an import statement. With this plugin you just place the cursor on the class that needs imported and press Ctrl + Option + I. Here's the github project. https://github.com/cppforlife/CedarShortcuts
I made a small Xcode plugin to sort the headers and remove duplicates of the file with a shortcut, you can check it out! - https://github.com/insanoid/CleanHeaders-Xcode
AppCode from Jetbrains can do this.
Yup. Like in Eclipse, this would be an awesome feature since developing in XCode (Cocoa Touch) does require quite a bit of class imports which are not easy to remember and Android development in Eclipse sorts this out with a simple keystroke combination that saves so much time ! Hope there is a way to do this in XCode soon !
WordService (freeware) from Devon Technologies, provides a service that can be used in any Cocoa app that can (amongst others) sort lines.
Alternatively, you can use an Xcode Extension, such as Imp
Swiftlint has an opt-in rule which if opted, will automatically sort the imports alphabetically.
Add - sorted_imports to your .swiftlint.yml file under opt_in_rules.
Run swiftlint autocorrect terminal command on project root directory (same where swiftlint.yml is stored).

Resources