开发者

A way to automatically organize #imports in Xcode

开发者 https://www.devze.com 2022-12-14 13:26 出处:网络
I love the \"Organize Imports\" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or Act开发者_开发技巧ionScript).

I love the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or Act开发者_开发技巧ionScript).

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:

  1. Start Automator -> New
  2. Choose "Quick Action" (or "Service" on older MacOS/Automator versions)
  3. add a "run shell script" action
  4. use sort | uniq as the script and check the "output replaces selected text" checkbox
  5. save and give it a name (e.g. "sort & unique")
  6. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消