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

Enumerations, structure and class IN SWIFT

  • 1.
    Enumerati on • An enumerationdefines 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 valueof 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.
  • 3.
  • 4.
    Enumeration Example enum WeakDays { case Monday caseTuesday case Wednesday case Thursday case Fridaycase Saturday, Sunday} case keyword is used to introduce new enumeration cases.
  • 5.
    Enumeration Example • Once youassign 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 { caseeast, 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 enumnumbers: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 enumfruitColor: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 enumnumbers: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 valuesenumfruitColor: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 valuesenumarea: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 valuesenumarea: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 VsRaw 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 candefine 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 encapsulatesdata 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 { varfName: 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 classis 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 classis 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 enablean 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 { varage = 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 ExampleclassPerson { 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{ varfName: 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 Examplevarcnt = 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 tofind 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 { varage = 20 } var ram = Person() print(ram.age) var sham = ram print(sham.ag e) print(ram === sham) Output 20 20 true false