• Clear your concept on Swift Enums and clousers through this tutorial
  • Get more skilled by learning in detail on Enums and closures in Swift Coding.

Learn iPhone Fundamentals

What are Enumerations

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. Through Enums you can declare a finite amount of items which help your swift code and intentions more clear.
As compare to enumerations in C and Objective-C,Here in Swift it is more flexible.Type safety is a important feature of the Swift language and enumerations fit perfectly in that mindset.

Enumerations in Swift are first-class types in their own right and they can store any type of value for each case. Here we define the ConnectionState enumeration.

    enum ConnectionState {
                    case Unknown
                    case Disconnected
                    case Connecting
                    case Connected
                }
                                    
The name of the enumeration is preceded by the enum keyword and followed by a pair of curly braces. We add members in enum with case keywords.Like c and Objective-C here enum does not assign integer values to these members. The members of the ConnectionState enums are values themselves and they are of type ConnectionState. This makes Swift's Enums safer and more explicit.

Assign Values

We can explicitly assign the values to each Item and It must be unique.

enum ConnectionState: Int {
case Unknown = -1
case Disconnected = 0
case Connecting = 1
case Connected = 2
}
                                
Here int is the type of enum.

Using Enums

Now we declare a variable, cState, and set its value to ConnectionState.Connecting.

var cState = ConnectionState.Connected

The value of connectionState is ConnectionState.Connected and the variable is of type ConnectionState.

Assosiated Values of Swift Enums

We can assign any assosiated value to Enums of any given type and value types can be different for each case. Now we declare a variable, cState, and set its value to ConnectionState.Connecting.

enum Employee {
    case Name(String)
    case Salary(Int, Int, Int)
}
                            


Now we create a two variable of Employee Enum like this.
var myName = Employee.Name("Peter")
var someNums = Employee.Salary(4000, 1200, 160)
                            

Closures

A closure is defined as a self-contained block of functionality which can be passed around and used in your code. Closures can capture and store references to any constants or variables from the context in which they are defined. This is know as closing over those variables, hence the name closures. These are intensively used in the Cocoa frameworks to develop iOS or Mac applications.

Closure Expression Syntax

Closure expression syntax has the following general form:
{ (parameters) -> return type in
    statements
}
                                
// Declare the variable myVar1, with data types, and assign value.
 
var myVar1 : () -> () =  {
     
    print("Hello from Closure 1");
}
 
// Declare the variable myVar2, with data types, and assign value.
 
var myVar2 : () -> (String)  = { () -> (String) in
 
    return "Hello from Closure 2"
 
}
 
// Declare the variable myVar3, with data types, and assign value.
 
var myVar3 : (Int, Int) -> (Int)  =  { (a : Int, b: Int) -> (Int) in
     
    var c : Int =  a + b
     
    return c
}
     
 
func test_closure()     {
     
    // Execute Closure.
    myVar1()
     
    // Execute Closure, and get returns value.
    var str2 = myVar2()
     
    print(str2)
     
    // Execute closure, pass parameters
    // and get returns value.
    var c: Int =  myVar3(11, 22)
     
    print(c)
     
}
                                

function is a special case of Closure.

Software Training & Solution Provider
258,Katewa Nagar,Jaipur,Rajasthan.
It's the near by location of Gurjar ki thadi, MANSAROVAR.
Ph: 9829708506 , 0141-4108506 , 08432830240, 8432706556

09829708506 , 08432830240