Tag: enumeration
-
What is Initialization in Swift?
Initialization is the process of preparing an instance of class, structure or enumeration for use. This process is implemented by using initializers, which are special methods that can be called to create new instance of particular type. Initialization involves setting up initial values for stored properties and performing any other set up or initialization as […]
-
How to modify value types within instance methods in Swift?
Properties of value types such as Structures and enumerations cannot be modified within instance methods. In order to modify their property, we need to add the keyword mutating before the instance method. Now the method can mutate(change) the properties and write back the changes to the original structure at the end of the method. The method […]
-
Swift: Recursive enumeration
Recursive enumeration in Swift is a enumeration which has a instance of another enumeration as one of the associated values of the enumeration cases. We indicate recursive enumerations with the keyword indirect. If all the cases in the enumeration has one or more instance of another enumeration, the keyword indirect can be place in the enum definition itself. […]
-
Swift: Initializing from rawValue
When we define a enumeration with a raw value type, Swift provides a default initializer that takes the value of that type’s raw value as a parameter and return a enumeration case or nil. Not all raw values will find a matching case in enumeration. Swift code
-
Swift: Implicitly assigned rawValues
In enumerations, we can provide default value for the cases. The values must be of same type. If we are to provide String values and Int values as default values to the enum cases, we don’t have to explicitly provide them.Swift can implicitly assign raw values for the enum cases.For string type, the name provided […]
-
Swift: Difference between rawValue and associated value in enumeration
Associated values allows as to store additional custom information along with case values which can be altered every time when we use that case in our code. Each case can have different combination of associated values. Enumeration cases can be pre-populated with default values called rawValues which must be of same type. Raw value for […]
-
Enum showing RawRepresentable error on using CaseIterable protocol
CaseIterable protocol is used to access the entire collection of values contained within a type if that type conforms to this protocol. This is done by using the allCases property. This works only on Xcode 10 and not in any lower versions.
-
Swift: How to access all the enumeration cases using CaseIterable protocol?
Types that conform to ‘CaseIterable protocol’ can let us access their entire collection of values by using their ‘allCases’ property. For example, enumeration type without associated values.Sometimes we need to get the value of all the cases contained in a enumeration type. First we have to declare that our enumeration conforms to CaseIterable protocol. Then […]
-
Swift: How to use associated values in enumeration ?
Enumeration in Swift can store unique associated values of different types for individual cases. The values may belong to any type such as ( Int, Double, Float or String). The above code example can be explained as follows: Define a enumeration of type CustomerKYC which can take one of the four values such as: dob, […]
-
Swift: How to extract associated values in enumeration?
Enumeration cases can have associated values belonging to different types. By using Switch statement we can extract the associated values for constants and variables declared as enum type.