一种在 Xcode 中自动组织 #imports 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1905347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A way to automatically organize #imports in Xcode
提问by Arrel
I love the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or ActionScript).
我喜欢 Eclipse 中的“组织导入”命令,它可以隐式地添加和删除导入到源文件中的类(如在 Java 或 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?
Xcode 中是否有命令#import
可以根据文件中引用的类更新.m Objective-C 文件顶部的指令?
回答by Tim Büthe
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:
您可以通过创建 Automator 操作并在 Xcode 以及 Mac OS X 中的任何地方使用它来完成此操作。为此,请执行以下操作:
- Start Automator -> New
- Choose "Service"
- 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"
- 启动 Automator -> 新建
- 选择“服务”
- 添加“运行 shell 脚本”操作
- 使用
sort | uniq
的脚本,并勾选“输出内容替换选定的文本”复选框 - 保存并为其命名(例如“排序和唯一”)
- 选中“输出替换所选文本”
After you saved it, you can just select your imports in Xcode, right click and choose your "sort & unique" action to organize your imports.
保存后,您只需在 Xcode 中选择您的导入,右键单击并选择您的“排序和唯一”操作来组织您的导入。
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.
这不如 Eclipse 或 IntelliJ 中的组织导入操作好,因为它不会删除未使用的东西等,但总比没有好。
PS: Got that from WWDC 2012 Sessions - Session 402 Working Effeciently with Xcode (starting at 6:15)
PS:从 WWDC 2012 Sessions - Session 402 Working Efficiently with Xcode(从 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/.
我开始使用 AppCode,它是 JetBrains 的 Objective-C IDE,它具有“组织导入”等功能。你应该检查一下:http: //www.jetbrains.com/objc/。
回答by Arrel
I'm resolving this as not currently a feature of Xcode :(
我正在解决这个问题,因为它目前不是 Xcode 的一个功能:(
回答by jstewart379
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
我发现 Cedar Shortcuts 至少是可用的。它不如 Eclipse 的导入功能好(它不组织导入),但它可以使您不必转到类文件的顶部并键入导入语句。使用此插件,您只需将光标放在需要导入的类上,然后按 Ctrl + Option + I。这是 github 项目。https://github.com/cppforlife/CedarShortcuts
回答by insanoid
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
我制作了一个小的 Xcode 插件来对标题进行排序并使用快捷方式删除文件的重复项,您可以查看它!- https://github.com/insanoid/CleanHeaders-Xcode
回答by Tim Büthe
AppCode from Jetbrainscan do this.
Jetbrains 的 AppCode可以做到这一点。
回答by Kumar C
Swiftlinthas an opt-in rule which if opted, will automatically sort the imports alphabetically.
Swiftlint有一个选择加入规则,如果选择加入,将自动按字母顺序对导入进行排序。
Add - sorted_imports
to your .swiftlint.yml file under opt_in_rules
.
添加- sorted_imports
到 .swiftlint.yml 文件下opt_in_rules
。
Run swiftlint autocorrect
terminal command on project root directory (same where swiftlint.yml is stored).
swiftlint autocorrect
在项目根目录(存储 swiftlint.yml 的位置)上运行终端命令。
回答by Johan Kool
WordService(freeware) from Devon Technologies, provides a service that can be used in any Cocoa app that can (amongst others) sort lines.
来自 Devon Technologies 的WordService(免费软件)提供了一项服务,该服务可用于任何可以(除其他外)对行进行排序的 Cocoa 应用程序中。
回答by Rajive Jain
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 !
是的。就像在 Eclipse 中一样,这将是一个很棒的功能,因为在 XCode(Cocoa Touch)中开发确实需要相当多的类导入,这并不容易记住,而 Eclipse 中的 Android 开发通过一个简单的按键组合来解决这个问题,从而节省了大量时间!希望很快有一种方法可以在 XCode 中做到这一点!