SlideShare a Scribd company logo
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
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.
Enumeration
Syntax
• Syntax:
enum EnumerationName:
RawType
{
// enumeration definition goes
here
}
Enumeration
Example
enum WeakDays
{
case Monday
case Tuesday
case
Wednesday
case Thursday
case Fridaycase Saturday,
Sunday}
case keyword is used to
introduce new
enumeration cases.
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
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
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
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
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
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
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.
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
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
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)
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.
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.
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
Structures with
subscriptstruct multi
{ let m: Int
subscript(i: Int) ->
Int
{
return m * i
}
}
let result = multi(m:
2) print(result[8])
Output
16
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
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
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
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.
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
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.
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 =
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
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
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 (!==)
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
Identity Operator
Exampleclass Person
{
var age = 20
}
var ram = Person()
print(ram.age)
var sham =
ram
print(sham.ag
e)
print(ram ===
sham)
Output
20
20
true
false

More Related Content

Similar to Enumerations, structure and class IN SWIFT

Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
Maclennan chap5-pascal
Maclennan chap5-pascalMaclennan chap5-pascal
Maclennan chap5-pascal
Serghei Urban
 

Similar to Enumerations, structure and class IN SWIFT (20)

enums
enumsenums
enums
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
enum_namespace.ppt
enum_namespace.pptenum_namespace.ppt
enum_namespace.ppt
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
 
Basic of java 2
Basic of java  2Basic of java  2
Basic of java 2
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Maclennan chap5-pascal
Maclennan chap5-pascalMaclennan chap5-pascal
Maclennan chap5-pascal
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
c unit programming arrays in detail 3.pptx
c unit programming arrays in detail 3.pptxc unit programming arrays in detail 3.pptx
c unit programming arrays in detail 3.pptx
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Json demo
Json demoJson demo
Json demo
 
Python ppt
Python pptPython ppt
Python ppt
 

More from LOVELY PROFESSIONAL UNIVERSITY

More from LOVELY PROFESSIONAL UNIVERSITY (19)

Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
 
Control structures IN SWIFT
Control structures IN SWIFTControl structures IN SWIFT
Control structures IN SWIFT
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
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.
  • 4. Enumeration Example enum WeakDays { case Monday case Tuesday case Wednesday case Thursday case Fridaycase Saturday, Sunday} case keyword is used to introduce new enumeration cases.
  • 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
  • 18. Structures with subscriptstruct multi { let m: Int subscript(i: Int) -> Int { return m * i } } let result = multi(m: 2) print(result[8]) Output 16
  • 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
  • 30. Identity Operator Exampleclass Person { var age = 20 } var ram = Person() print(ram.age) var sham = ram print(sham.ag e) print(ram === sham) Output 20 20 true false