A way to automatically organize #imports in Xcode

2019-03-08 14:57发布

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?

8条回答
▲ chillily
2楼-- · 2019-03-08 15:29
混吃等死
3楼-- · 2019-03-08 15:33

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

查看更多
我只想做你的唯一
4楼-- · 2019-03-08 15:36

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

查看更多
戒情不戒烟
5楼-- · 2019-03-08 15:37

Alternatively, you can use an Xcode Extension, such as Imp

查看更多
劳资没心,怎么记你
6楼-- · 2019-03-08 15:38

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 "Service"
  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/.

查看更多
手持菜刀,她持情操
7楼-- · 2019-03-08 15:43

I'm resolving this as not currently a feature of Xcode :(

查看更多
登录 后发表回答