Swift Enum With Code Examples

  • Updated
  • Posted in Programming
  • 5 mins read


Swift Enum With Code Examples

With this piece, we’ll check out just a few totally different examples of Swift Enum points within the laptop language.

enum WeatherType {
    case solar
    case cloud
    case rain
    case wind
    case snow
}

func getHaterStatus(climate: WeatherType) -> String? {
    if climate == .solar {
        return nil
    } else {
        return "Hate"
    }
}

getHaterStatus(climate: .cloud)

There are many alternative approaches to fixing the identical downside Swift Enum. The following part discusses the varied different potential options.

Struct : Value Type | No Inheritance | No Deinitializers | No Type casting
Class : Reference Type | Inheritance supported | Has Deinitializers | Type casting

Common components between struct and sophistication:
Both permits to outline properties to retailer values
Both permits to outline strategies
Both permits to outline subscripts 
Both permits to outline initializers
Both permits extension and protocols 
func simplyAnInstance<T : RawRepresentable>(_ enumType: T.Type) the place T.UncookedValue == Int {

  // Note that an express use of init is required when creating an occasion from a
  // metatype. We're additionally utilizing a guard, as `init?(uncookedValue:)` is failable.
  guard let val = enumType.init(uncookedValue: 0) else { return }
  print("description: (val)")
}

simplyAnInstance(Foo.self) // prints: "description: Hello Foo"
simplyAnInstance(Bar.self) // prints: "description: Hello Bar"
// There is not any manner to do that. But there's a little workaround.

// Swift 5:
public enum Size: String {
  case small = "small"
  case medium = "medium"
  case massive = "massive"
}

class SizeView: UIView {
  non-public var dimension: Size = .massive

  @IBInspectable 
  public var sizeName: String = "massive" {
    didSet {
      guard self.sizeName != outdatedValue else { return }
      self.dimension = Size(uncookedValue: self.sizeName)
    }
  }
}
enum Season {
  case spring, summer time, autumn, winter
}

We have demonstrated, with a plethora of illustrative examples, learn how to sort out the Swift Enum downside.

What is Swift enum?

What is a Swift Enum? According to the Swift documentation enumeration is outlined as “a typical sort for a gaggle of associated values and allows you to work with these values in a type-safe manner inside your code”. Think of it as a sort of variable that’s particularly used change/conditionals.22-Nov-2019

What is distinction between enum and struct in Swift?

Coming from an goal c background, the distinction between parts like enums, lessons, and structs was fairly apparent for me: An Enum is a set of named values, Struct is structured knowledge sort, and naturally Class permits us to create objects with all POO associated stuff.

Can enum have features Swift?

Enumerations (or enums for brief) in Swift outline a typical sort for a gaggle of associated values. According to the Swift documentation, enums allow you to work with these values in a type-safe manner inside your code. Enums are available notably helpful when you may have loads of totally different choices you need to encode.28-May-2021

Is enum worth sort in Swift?

Types in Swift fall into considered one of two classes: first, “worth typesvalue varietiesIn laptop programming, knowledge varieties will be divided into two classes: worth varieties and reference varieties. A price of worth sort is the precise worth. A price of reference sort is a reference to a different worth.https://en.wikipedia.org › Value_type_and_reference_typeValue sort and reference sort – Wikipedia”, the place every occasion retains a singular copy of its knowledge, often outlined as a struct, enum, or tuple. The second, “reference varieties”, the place situations share a single copy of the info, and the sort is often outlined as a category.15-Aug-2014

What is enum in Swift with instance?

In Swift, an enum (brief for enumeration) is a user-defined knowledge sort that has a set set of associated values. We use the enum key phrase to create an enum. For instance, enum Season { case spring, summer time, autumn, winter } Here, Season – identify of the enum.

What is the usage of enum in IOS?

An enumeration is a knowledge sort consisting of a set of named values, referred to as members. Apple doc says: An enumeration defines a typical sort for a gaggle of associated values and allows you to work with these values in a type-safe manner inside your code.

What is distinction between enum and sophistication?

An enum can, identical to a category , have attributes and strategies. The solely distinction is that enum constants are public , static and closing (unchangeable – can’t be overridden). An enum can’t be used to create objects, and it can not lengthen different lessons (however it will possibly implement interfaces).

What are enum varieties?

Enumerated (enum) varieties are knowledge varieties that comprise a static, ordered set of values. They are equal to the enum varieties supported in a lot of programming languages. An instance of an enum sort could be the times of the week, or a set of standing values for a bit of information.

Can you lengthen an enum Swift?

We can lengthen the enum in two orthogonal instructions: we are able to add new strategies (or computed properties), or we are able to add new instances. Adding new strategies will not break present code. Adding a brand new case, nevertheless, will break any change assertion that does not have a default case.07-May-2019

Can enums inherit Swift?

In Swift language, we now have Structs, Enum and Classes. Struct and Enum are handed by copy however Classes are handed by reference. Only Classes help inheritance, Enum and Struct do not.17-Oct-2015

Leave a Reply