OptionSet usage in Swift like NS_OPTIONS – onlinecode

OptionSet usage in Swift like NS_OPTIONS – onlinecode

In this post we will give you information about OptionSet usage in Swift like NS_OPTIONS – onlinecode. Hear we will give you detail about OptionSet usage in Swift like NS_OPTIONS – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Swift 3.0 introduced the OptionSet protocol after renaming from OptionSetType. It is a Swift alternative to the well-known and often used NS_OPTIONS in Objective-C. Swift imports NS_OPTIONS types as conforming to the OptionSet protocol. It presents a set-like interface for options:

struct CoffeeManipulators : OptionSet {
    let rawValue: Int

    static let milk     = CoffeeManipulators(rawValue: 1 << 0)
    static let sugar    = CoffeeManipulators(rawValue: 1 << 1)
    static let milkAndSugar = [milk, sugar]

The contains method is used to check whether an option is used. This makes it easy to add accessor methods like in the following example the hasMilk and hasManipulators methods are.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.
struct Coffee {
    let manipulators: [CoffeeManipulators]

    // You can now simply check if an option is used with contains
    func hasMilk() -> Bool {
        return manipulators.contains(.milk)

    func hasManipulators() -> Bool {
        return manipulators.count != 0

This can be handy if you defined a certain class or view which is able to set-up with different options.


Hope this code and post will helped you for implement OptionSet usage in Swift like NS_OPTIONS – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US