Is it possible to abbreviate this condition in Swift?


Is there a way to abbreviate the following type of condition in Swift?

if ( (myEnum == .1 || myEnum == .2 || myEnum == .3 || myEnum == .8) && startButton.isSelected )

I tried typing:

if ( (myEnum == .1 || .2 || .3 || .8) && startButton.isSelected )


if ( (myEnum == .1,.2,.3,.8) && startButton.isSelected )

but none of those worked. I also tried looking at documentation but can't find a similar example.



I don't think there is a way to abbreviate it like you want but there is, perhaps, another way to approach the same thing...

extension YourEnum { var someCondition: Bool { switch self { case .1, .2, .3, .8: return true default: return false } } }

By doing this your condition at the call site becomes...

if myEnum.someCondition, startButton.isSelected { doTheThing() }

By using this approach your code become more readable too. You can now give your condition a sensible name which other developers (including your future self) will be able to understand. Where before I would have no idea why those cases were chosen.

It also allows you to use this condition in multiple places and have only one implementation of it. So if the requirement changes you can change it in one place.


if [.a, .b, .c, .d].contains(myEnum) && startButton.isSelected { // etc. }


For this case I like to use John Sundell's extension to equatable:

extension Equatable { func isAny(of candidates: Self...) -> Bool { return candidates.contains(self) } }

You can then use it as:

if myEnum.isAny(of: .1, .2, .3, .8) && startButton.isSelected { .. }


  • Is it possible to conditionally hide properties at compile time in .Net?
  • ZEND FW : Joining two tables from different databases
  • What is the easiest way to load a filtered .tda file using pandas?
  • Multiple Thread Variable Access inside a function
  • VB.NET 2012 Property Set on Property Get
  • Deleting a widget from QTableView
  • How to know which Linq statement produced the SQL on hand during runtime?
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Easiest way to get current unix timestamp via XSL
  • Build Successful but not running on simulator
  • How to get current document uri in XSLT?
  • Copy to all folders batch file?
  • Create DicomImage from scratch using Dcmtk
  • debug library loaded with ctypes using gdb
  • Moving mysql files across servers
  • How to install a .deb file on a jailbroken iphone programmatically?
  • How to use an array of arrays with array_map(…) in PHP?
  • How to do unit test for HttpContext.Current.Server.MapPath
  • How to rebase a series of branches?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Modifying destination and filename of gulp-svg-sprite
  • Perl system calls when running as another user using sudo
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Adding custom controls to a full screen movie
  • GridView Sorting works once only
  • SVN: Merging two branches together
  • How to delete a row from a dynamic generate table using jquery?
  • Load html files in TinyMce
  • using HTMLImports.whenReady not working in chrome
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Why do underscore prefixed variables exist?