Swift Tutorial - Tutorialspoint

(backadmin) #1
case .India:
println("Climate is Hot")
case .America:
println("Climate is Cold")
case .Africa:
println("Climate is Moderate")
case .Australia:
println("Climate is Rainy")
default:
println("Climate is not predictable")
}

When we run the above program using playground, we get the following result:


Climte is Cold

The program first defines Climate as the enumeration name. Then its members like 'India',
'America', 'Africa' and 'Australia' are declared belonging to class 'Climate'. Now the
member America is assigned to a Season Variable. Further, Switch case will see the values
corresponding to .America and it will branch to that particular statement. The output will
be displayed as "Climate is Cold". Likewise all the members can be accessed through
switch statements. When the condition is not satisfied it prints by default 'Climate is not
predictable'.


Enumeration can be further classified in to associated values and raw values.


Difference between Associated Values and Raw Values


Associated Values Raw Values

Different Datatypes Same Datatypes

Ex: enum {10,0.8,"Hello"} Ex: enum {10,35,50}

Values are created based on constant or variable Prepopulated Values

Varies when declared each time Value for member is same

Enum with Associated Values


enum Student{
case Name(String)
case Mark(Int,Int,Int)
}
Free download pdf