如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:42:32  来源:igfitidea点击:

How to autocomplete enum switch using Xcode?

iosxcodeenums

提问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 语句片段:

enter image description here

在此处输入图片说明

and then:

进而:

enter image description here

在此处输入图片说明

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

SCXcodeSwitchExpander

SCXcode开关扩展器

回答by Joey

Short Answer :

简答:

Use Editor> Refactor> Add Missing Switch Cases

使用Editor> Refactor>Add Missing Switch Cases

Xcode screenshot

Xcode 截图

Convenient way :

方便的方式:

  1. Open Preferencesby pressing [command]+ [,]in Xcode.
  2. Go to Key Bindingstab and set a shortcut for the command Refactor > Add Missing Switch Cases. xcode preferences screenshot
  3. Input the shortcut in editor. Be sure the cursor is on switchletters. xcode gif
  1. 在 Xcode 中Preferences[command]+打开[,]
  2. 转到Key Bindings选项卡并为命令设置快捷方式Refactor > Add Missing Switch Casesxcode 首选项截图
  3. 在编辑器中输入快捷方式。确保光标在switch字母上。 xcode gif

回答by toxicsun

in Xcode 10 do the following

在 Xcode 10 中执行以下操作

  1. add switch...casetemplate and set your enum type instead of selfso that xcode understands what it should autocomplete
  1. 添加switch...case模板并设置您的枚举类型,而不是self让 xcode 了解它应该自动完成的内容

  1. move cursor inside word switchand go to menu > editor > refactor > expand switch cases
  1. 在单词内移动光标switch并转到menu > editor > refactor > expand switch cases

This is what you will get as result

这就是你会得到的结果

回答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 是不可能的。提交错误报告并选择请求增强选项。

https://bugreport.apple.com

https://bugreport.apple.com

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 语句,但有一些技巧可以加快编写它们的速度。这是我最近一直在做的事情:

  1. Start the switch autocomplete, and type the expression
  2. 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)
  3. Move the cursor to before the first constant placeholder
  4. Press TAB, then the first letter of the constant, then Enter to accept the first autocomplete suggestion
  5. 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.
  1. 启动 switch 自动完成,并输入表达式
  2. 复制 case 块并粘贴足够的次数以覆盖所有枚举常量(实际上,我只是粘贴了比我需要的更多并稍后删除,而不是计数)
  3. 将光标移动到第一个常量占位符之前
  4. 按 T​​AB,然后是常量的第一个字母,然后按 Enter 接受第一个自动完成建议
  5. 对其余部分重复 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 ~/binfolder which i place helpers like this and chmod +xthem 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 Despotovic

Just hit "Fix" button and the cases will appear: enter image description here

只需点击“修复”按钮,就会出现案例: 在此处输入图片说明

回答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

enter image description here

在此处输入图片说明

Hope it is helpful!

希望它有帮助!