Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autocomplete enum switch using Xcode?

Tags:

enums

xcode

ios

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:

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?

like image 713
Artur Kucaj Avatar asked Feb 28 '14 13:02

Artur Kucaj


3 Answers

Short Answer :

Use Editor > Refactor > Add Missing Switch Cases

Xcode screenshot

Convenient way :

  1. Open Preferences by pressing [command] + [,] in Xcode.
  2. Go to Key Bindings tab 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 switch letters. xcode gif
like image 94
Joey Avatar answered Nov 10 '22 19:11

Joey


Saw your question and decided to build an Xcode plugin that does exactly that.

Check it out: https://github.com/stefanceriu/SCXcodeSwitchExpander

SCXcodeSwitchExpander

like image 20
Stefan Avatar answered Nov 10 '22 19:11

Stefan


in Xcode 10 do the following

  1. add switch...case template and set your enum type instead of self so that xcode understands what it should autocomplete

  1. move cursor inside word switch and go to menu > editor > refactor > expand switch cases

This is what you will get as result

like image 9
toxicsun Avatar answered Nov 10 '22 20:11

toxicsun