The document discusses Swift enumerations and structures. It provides details on:
- How enumerations define a common type for a set of related values and are more flexible than other languages.
- The syntax for defining enumerations and examples of enumerations with and without raw values.
- The differences between associated values and raw values in enumerations.
- How structures define properties and methods to encapsulate related data and provide initializers to set up their initial state.
Salient features of Environment protection Act 1986.pptx
Enumerations, structure and class IN SWIFT
1. Enumerati
on
• An enumeration defines a common type
for a group of related values.
• Enumerations in Swift are much more
flexible as
compared to other languages.
• You do not have to provide a value for
each
case of the enumeration.
• enum keyword is used to define an
2. Enumerati
on
• The value of each case in enumeration
can be a String, a character, an Integer or
floating point number is called associated
value.
• Enumerations are preferred over structures,
when there is finite set of values.
• Similar to other languages enumerations in
Swift can also have raw values.
5. Enumeration
Example
• Once you assign to an enum value, you
can reassign to another value without re-
specifying the enum name.
• Example:
var enumVar =
enum.case1 enumVar
= .case2
6. Enumeration
Exampleenum direction
{ case east, west, north,
south } var move =
direction.east switch move
{ case .east:
print("Moving to
east")
case .west:
print("Moving to
west")
case .north:
print("Moving to north")
case .south:
Output
Moving to east
7. Enumeration with raw
values
enum numbers:Int
{
case one = 1
case two = 2
case three =
3
}
print(numbers.one.rawValu
e)
print(numbers.two.rawValu
e)
Output
1
2
3
8. Enumeration with raw
values
enum fruitColor:String
{
case mango =
"Yellow"
case apple = “Red"
}
print(fruitColor.mango.rawVal
ue)
print(fruitColor.apple.rawValu
e)
Output
Yellow
Red
9. Enumeration with raw
values
enum numbers:Int
{
case one = 1
case two,
three
}
print(numbers.one.rawValu
e)
print(numbers.two.rawValu
e)
print(numbers.three.rawVal
Output
1
2
3
Note: When integers are used for raw values, they
auto-increment if no value is specified
10. Enumeration with raw
valuesenum fruitColor:String
{
case mango =
"Yellow" case apple
= "Red" case
orange
}
print(fruitColor.mango.rawVal
ue)
print(fruitColor.apple.rawValu
e)
Output
Yellow
Red
orange
Note: When Strings are used for raw values, they
itself treat as raw value if no value is specified
11. Enumeration with raw
valuesenum area:Float
{
case length = 1.0
case breadth
}
print(area.length.rawValue
)
print(area.breadth.rawValu
e)
Output
Error
Note: When non-integers are used for raw values
once, it is must to assign raw value for each case.
12. Enumeration with raw
valuesenum area:Float
{
case length
case breadth
case
dimensions
}
print(area.length.rawValue)
print(area.breadth.rawValue)
print(area.dimensions.rawVa
lue)
Output
0.0
1.0
2.0
13. Enumeration with Associated
valuesenum
enum1{ case
name(String)
case
ageSal(Int,Int)
}
var person =
enum1.name("Raman")
person = .ageSal(26,30000)
switch person
{ case .name(let n):
print("Welcome (n)")
case .ageSal(let n, let r):
print("Age: (n), Salary:
Output
Age: 26
Salary: 30000
14. Associated Values Vs Raw
Values
Associated Values Raw Values
Different Datatypes
E.g: enum {10,0.8,"Hello"}
Same Datatypes
E.g: enum {10,35,50}
Values are created based
on constant or
variable
Values should be literals
only
Varies when declared
each
time
Value for member is same,
can’t
be changed (i.e. immutable)
15. Structur
es
• Structure can define properties to store
values.
• Structure can define methods to
provide functionality.
• Structure are always copied when they
pass around in the code.
• Structure can define initializers to set up
their initial state.
16. Structur
es• Structure encapsulates data some related
data values.
• Structure pass their members by the values
not by
reference.
• Structure can define subscripts to provide
access
to their values using subscript syntax.
17. Structures
Example
struct Laptop
{
var color =
"Black" var
processor = "i7"
var price = 38000
}
var dellD15 =
Laptop()
print(dellD15.color)
print(dellD15.process
Output
Black
i7
38000
19. Structures
Example
struct Person {
var fName:
String
var lName:
Stringinit(fName: String, lName: String)
{ self.fName =
fName self.lName
= lName
}
}
var john = Person(fName: "John", lName:
Output
John' last name is Watson
20. Class
es
• A class is a blueprint or template for an
instance
of that class.
• The term "object" is often used to refer to
an
instance of a class.
• In Swift, however, classes and structures
are very similar, and therefore it's easier
and less confusing to use the term
21. Class
es
• A class is collection of properties and
methods.
• Classes in Swift are of reference type.
• Classes having all the things that structure
have
and some additional features also.
• Type casting enables you to check and
interpret the type of a class instance at
22. Class
es
• Initializer enable an instance of class to
initialize
with some values.
• De-initializers enable an instance of a class
to
free up any resources it has assigned.
• Reference counting allows more
than one reference to a class
instance.
23. Class
Exampleclass Person
{ var age = 20
var gender =
"Male" var name =
"Raman" func
show()
{ print(age)
print(gende
r)
print(name)
}
}
Output
20
Male
Raman
24. Initializer and De-
initializer
• Initializer enable an instance of class to
initialize
with some values.
• To create an initial value.
• To assign default property value within the
property definition.
• To initialize an instance for a particular data type 'init()'
is
used.
• De-initializers enable an instance of a class
to
free up any resources it has assigned.
25. Class with init
Exampleclass Person
{
var age:Int
var gender:String
var name:String
init()
{ age = 20
gender =
"male" name
= "Raman"
}
Output
}
20
Male
Raman
func show()
{ print(age)
print(gende
r)
print(name)
}
var ob =
26. init having
parametersclass
Person{ var fName: String
var lName: String
init(fName: String, lName:
String){ self.fName =
fName
self.lName =
lName
}
func
show()
{print("Welcome (fName) (lName).")
}
}
var ram =
Person(fName:"Ram",lName:"Sharma")
ram.show()
Output
Welcome Ram Sharma.
self property to refer to
the current instance
27. init and deinit
Examplevar cnt = 0 // for reference
counting class myClass
{
init()
{ cnt = cnt+1 }
deinit
{ cnt = cnt-1 }
}
var ob:myClass? =
myClass()
print(cnt)
ob = nil
print(cn
Output
1
0
28. Identity
Operator
• Used to find out if two constants or
variables refer to exactly the same
instance of a class or not.
• For this two types of operators are there:
• Identical to (===)
• Not identical to (!==)
29. Identity
Operatorclass Person
{
var age = 20
}
var ram = Person()
print(ram.age)
var sham =
Person()
print(sham.age)
print(ram ===
sham) print(ram
!== sham)
Output
20
20
false
true