如何使用 Xcode 自动完成枚举切换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22096852/
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
How to autocomplete enum switch using Xcode?
提问by Artur Kucaj
Let say you have an enum:
假设你有一个枚举:
typedef enum {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
} DayOfWeek;
when writing a switch statement, a Xcode is trying to help with switch statement snippet:
在编写 switch 语句时,Xcode 试图帮助处理 switch 语句片段:
and then:
进而:
great, but I need to list all enums:
太好了,但我需要列出所有枚举:
DayOfWeek day = ...; // a day of week
switch (day) {
case Sunday:
break;
case Monday:
break;
case Tuesday:
break;
case Wednesday:
break;
case Thursday:
break;
case Friday:
break;
case Saturday:
break;
default:
break;
}
Unfortunately, have to do it manually :( Is there any known snippet to populate all cases? Of course I saw some answers 3 years old states that it is impossible, but maybe something changed since that time? Any ideas?
不幸的是,必须手动进行 :( 是否有任何已知的片段来填充所有案例?当然我看到一些 3 年前的答案说这是不可能的,但也许自那时以来发生了一些变化?有什么想法吗?
采纳答案by Stefan
Saw your question and decided to build an Xcode plugin that does exactly that.
看到您的问题并决定构建一个可以做到这一点的 Xcode 插件。
Check it out: https://github.com/stefanceriu/SCXcodeSwitchExpander
看看:https: //github.com/stefanceriu/SCXcodeSwitchExpander
回答by Joey
Short Answer :
简答:
Use Editor
> Refactor
> Add Missing Switch Cases
使用Editor
> Refactor
>Add Missing Switch Cases
Convenient way :
方便的方式:
回答by toxicsun
回答by Tim
Unfortunately this is not possible with Xcode out of the box. File a bug report and select the request an enhancement option.
不幸的是,这对于开箱即用的 Xcode 是不可能的。提交错误报告并选择请求增强选项。
I actually did this myself lately, and they gauge the severity and importance of bugs/enhancements by the number of duplicates they receive for a given issue - hence one of the reasons for not being able to search the tracker.
我实际上最近自己做了这个,他们通过他们收到的给定问题的重复数量来衡量错误/增强的严重性和重要性——因此这是无法搜索跟踪器的原因之一。
回答by Erwin
While Xcode won't autocomplete the entire switch statement, there are tricks to speeding up writing them. This is what I've been doing lately:
虽然 Xcode 不会自动完成整个 switch 语句,但有一些技巧可以加快编写它们的速度。这是我最近一直在做的事情:
- Start the switch autocomplete, and type the expression
- Copy the case block and paste it enough times to cover all the enum constants (actually, I just paste more than I need and delete later, rather than counting)
- Move the cursor to before the first constant placeholder
- Press TAB, then the first letter of the constant, then Enter to accept the first autocomplete suggestion
- Repeat TAB, letter, ENTER for the rest. Autocomplete won't suggest constants you've already used in the switch, so the list narrows with each use.
- 启动 switch 自动完成,并输入表达式
- 复制 case 块并粘贴足够的次数以覆盖所有枚举常量(实际上,我只是粘贴了比我需要的更多并稍后删除,而不是计数)
- 将光标移动到第一个常量占位符之前
- 按 TAB,然后是常量的第一个字母,然后按 Enter 接受第一个自动完成建议
- 对其余部分重复 TAB、字母、ENTER。自动完成不会建议您已经在 switch 中使用的常量,因此列表会随着每次使用而缩小。
Of course, it really helps if all the constants start with the same letter. In your case, consider naming them:
当然,如果所有常量都以相同的字母开头,那真的很有帮助。在您的情况下,请考虑命名它们:
typedef enum {
dayOfWeekSunday,
dayOfWeekMonday,
dayOfWeekTuesday,
dayOfWeekWednesday,
dayOfWeekThursday,
dayOfWeekFriday,
dayOfWeekSaturday
} enumDayOfWeek;
回答by Warren Burton
It's not using Xcode, but you can very quickly burp out processed snippets with any scripting language. Throw it as a plugin in your favourite text editor too (e.g Textmate
).
它不使用 Xcode,但您可以使用任何脚本语言快速生成处理过的片段。也可以将它作为插件投放到您最喜欢的文本编辑器中(例如Textmate
)。
I have a ~/bin
folder which i place helpers like this and chmod +x
them so i can use anytime like this:
我有一个~/bin
文件夹,我把这样的助手和chmod +x
他们放在里面,这样我就可以随时使用:
enumify.pl ~/tmp.txt
enumify.pl ~/tmp.txt
or even better use the command line pasteboard tools:
甚至更好地使用命令行粘贴板工具:
pbpaste | enumify.pl
pbpaste | enumify.pl
#! /usr/bin/perl
#enumify.pl
#reads from STDIN or use filename as first arg
#on OS X combine with pbpaste e.g pbpaste | enumify.pl
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
my @lines;
$filename = $ARGV[0];
if (length($filename) > 0) {
{ local *FILE; open FILE, "<$filename"; @lines = <FILE>; close FILE }
} else {
@lines = <STDIN>;
}
if (scalar @lines) {
my $target = "switch(<#switchvar#>) {\n";
my $enumlock = 0;
foreach (@lines) {
if ($enumlock == 0 && m/enum/) {
$enumlock = 1;
}
elsif ( $enumlock == 1) {
$_ = trim($_);
if (!(m/[{}]/) && (length($_)>0)) {
$_ =~ s/,//;
$target = "$target case $_:\nbreak;\n";
}
}
}
$target = "$target default:\nbreak;\n}\n";
if ( $enumlock == 1) {
print $target;
}
}
It turns
事实证明
typedef enum {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
} DayOfWeek;
into
进入
switch(<#switchvar#>) {
case Sunday:
break;
case Monday:
break;
case Tuesday:
break;
case Wednesday:
break;
case Thursday:
break;
case Friday:
break;
case Saturday:
break;
default:
break;
}
回答by timaktimak
Hey I made an Xcode 8 Source Editor Extension for that in Swift.
嘿,我在 Swift 中为此制作了一个 Xcode 8 源代码编辑器扩展。
https://github.com/timaktimak/SwitchCaseGenerator
https://github.com/timaktimak/SwitchCaseGenerator
Hope it is helpful!
希望它有帮助!