How to automatically organize #imports in Xcode

I like the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into the source file (like in Java or ActionScript).

Is there a command in Xcode to update the #import directives in top.m Objective-C files based on the classes the file refers to?

+41
import xcode keyboard-shortcuts
Dec 15 '09 at 5:28
source share
8 answers

You can do this by creating an Automator action and using it in Xcode, as well as everywhere in Mac OS X. To do this, follow these steps:

  • Launch Automator โ†’ New
  • Select "Service"
  • add action "run shell script"
  • use sort | uniq sort | uniq as a script and check the box "output replaces the selected text"
  • save and give it a name (for example, "sort and unique")
  • check "Exit replaces selected text"

After you save it, you can simply select your import in Xcode, right-click and select your "sort and unique" action to organize the import.

This is not as good as organizing import operations in Eclipse or IntelliJ, because it does not remove unused things, etc., but it is better than nothing.

PS: Got it from WWDC 2012 sessions - Session 402 Works efficiently with Xcode (starting at 6:15)

EDIT

I started using the AppCode, an Objective-C IDE from JetBrains, and it has features like "organize imports." You should check this out: http://www.jetbrains.com/objc/ .

+63
Jun 24 2018-12-12T00:
source share

I allow this since it is currently not an Xcode function :(

+4
Apr 26 '10 at 18:49
source share

I found Cedar Shortcuts, at least for use. This is not as good as the Eclipse import function (it does not order the import), but this may prevent you from going to the beginning of the class file and entering the import statement. With this plugin, you simply place the cursor in the class you want to import and press Ctrl + Option + I. Here's the github project. https://github.com/cppforlife/CedarShortcuts

+3
Jul 08 '13 at 18:18
source share

I made a small Xcode plugin to sort the headers and remove duplicate files using a shortcut, you can check it out! - https://github.com/insanoid/CleanHeaders-Xcode

+2
Oct 21 '15 at 12:43 on
source share

Jetbrains' AppCode can do this.

+1
Aug 21 2018-12-12T00:
source share

Yeah. Like Eclipse, this would be an amazing feature, since development in Xcode (Cocoa Touch) requires a fairly small amount of import of classes that are not so easy to remember, and Android development in Eclipse sorts it with a simple key combination, which saves a lot of time! Hope this happens soon in Xcode!

0
Aug 13 2018-12-18T00:
source share

WordService (freeware) from Devon Technologies provides a service that can be used in any Cocoa application that can (among others) sort lines.

0
Mar 19 '14 at 15:32
source share

Alternatively, you can use the Xcode extension, for example Imp

0
Aug 03 '17 at 12:37 on
source share



All Articles