OptionSet in Swift explained with code examples

Click for: original source

OptionSet in Swift allows you to define a set of options for configurations. It’s the Swift variant of the well-known NS_OPTIONS in Objective-C and it’s used throughout the standard libraries. By Antoine van der Lee.

The main focus of the article:

  • What is an OptionSet?
  • How to create an OptionSet
  • Reading values from an OptionSet

A set of options is often confused by a set of enum cases, but they’re not the same. While you could create a similar solution using enums, they’re meant to be used singularly. OptionSet in Swift allows you to define a set of configurations with a method or type initializer. Throughout the standard libraries, you can find examples of option sets to configure types like a JSONSerializer. By providing available options, you allow implementors of a type to configure instances in a readable manner without exposing too many implementation details. Good read!

[Read More]

Tags swiftlang app-development ux web-development