You are on page 1of 10

Class vs Struct vs Actor

# Class:
- A class is simply a representation of a type of object.It is the
blueprint/plan/template that describes the details of an object.
- An object is an instance of a class. It has its own state, behavior, and identity. An
object is simply a collection of data (variables) and methods (functions)

class Person {
var clothes: String
var shoes: String

init(clothes: String, shoes: String) {


self.clothes = clothes
self.shoes = shoes
}
func describe() {

print("I like wearing \(clothes) with \(shoes)")


}

let taylor = Person(clothes: "T-shirts", shoes: "sneakers")

let other = Person(clothes: "short skirts", shoes: "high heels")

var taylorCopy = taylor

taylorCopy.shoes = "flip flops"

print(taylor)
print(taylorCopy)

# Difference between class and object


An object is an instance of a class. Objects hold multiple information, but classes don’t
have any information. Definition of properties and functions can be done in class and can be
used by the object.

# Struct:

In Swift, a struct is used to store variables of different data types. For example,

Suppose we want to store the name and age of a person. We can create two variables: name
and age and store value.

However, suppose we want to store the same information of multiple people.

In this case, creating variables for an individual person might be a tedious task. To overcome
this we can create a struct that stores name and age. Now, this struct can be used for every
person.

struct Person {

var clothes: String

var shoes: String

func describe() {

print("I like wearing \(clothes) with \(shoes)")


}

let taylor = Person(clothes: "T-shirts", shoes: "sneakers")

let other = Person(clothes: "short skirts", shoes: "high heels")

var taylorCopy = taylor

taylorCopy.shoes = "flip flops"

print(taylor)
print(taylorCopy)

# Actor
Actors provide the same capabilities as all of the named types in Swift. They can have
properties, methods, initializers, subscripts, and so on. They can conform to protocols and be
augmented with extensions.
In the following chapters we’re going to explore more about how actors work and when you
should use them, but here is the least you need to know:

1. Actors are created using the actor keyword. This is a concrete nominal type in Swift, like
structs, classes, and enums.

2. Like classes, actors are reference types. This makes them useful for sharing state in
your program.
3. They have many of the same features as classes: you can give them properties,
methods (async or otherwise), initializers, and subscripts, they can conform to protocols,
and they can be generic.
4. Actors do not support inheritance, so they cannot have convenience initializers, and do
not support either final or override.
5. All actors automatically conform to the Actor protocol, which no other type can use. This
allows you to write code restricted to work only with actors.

actor User {

var score = 10

func printScore() {

print("My score is \(score)")

func copyScore(from other: User) async {

score = await other.score

}
}

let actor1 = User()

let actor2 = User()

await print(actor1.score)

await actor1.copyScore(from: actor2)


You can see several things in action there:

1. The new User type is created using the actor keyword rather than struct or class.
2. It can have properties and methods just like structs or classes.
3. The printScore() method can access the local score property just fine, because it’s our
actor’s method reading its own property.
4. But in copyScore(from:) we’re attempting to read the score from another user, and we
can’t read their score property without marking the request with await.
5. Code from outside the actor also needs to use await.

# Initializer
class Wall {
var length: Double
var height: Double

// parameterized initializer to initialize properties


init(length: Double, height: Double) {
self.length = length
self.height = height
}

func calculateArea() -> Double {


return length * height
}
}

// create object and initialize data members


var wall1 = Wall(length: 10.5, height: 8.6)
var wall2 = Wall(length: 8.5, height: 6.3)

print("Area of Wall 1: ", wall1.calculateArea())


print("Area of Wall 2: ", wall2.calculateArea())

Output:
Area of Wall 1: 90.3
Area of Wall 2: 53.55

#Initializer Overloading
class Person {
var age: Int

// 1. initializer with no arguments


init() {
age = 20
}

// 2. initializer with an argument


init(age: Int) {
self.age = age
}

// method to return age


func getAge() -> Int {
return age
}
}

var person1 = Person()


var person2 = Person(age: 23)

print("Person1 Age:", person1.getAge())


print("Person1 Age:", person2.getAge())

# convenience Initializer
class University {

var name : String


var rank : String

init(name : String, rank: String) {


self.name = name
self.rank = rank
}

// define convenience init


convenience init() {
self.init(name: "Kathmandu University", rank: "1st")
}

var university1 = University()


print(university1.name)
print("Rank:", university1.rank)

Output:
Kathmandu University
Rank: 1st
# Computed properties in struct

Structs can also declare computed properties. A computed property is, like its name implies, a
property that changes its value dynamically based on a computation. Here, check out this
example:

struct Circle {
var radius:Double
var circumference: Double {
return 2 * .pi * radius
}
}

let balloon = Circle(radius: 30)


print(balloon.circumference)

# When we use class over struct


The Apple official documentation largely recommended that users should use “struct” by
default.
This is mostly because structs are much safer and bug free, especially in a multithreaded
environment.

● Classes are reference type and structs are value type.


● If class inheritance is not needed, structs are faster and more memory efficient.
● Use struct for unique copies of an object with independent states.
● Use class to access objective-c runtime.
● Use class to control an object identity.
● Use structs when there is no need to control an object identity.

struct Coordinates {
var lat: Double
var lng: Double
}

var point1 = Coordinates(lat: 5.519, lng: 5.759)

var point2 = point1


point2.lat = 6.45
point2.lng = 8.211

print(point1)
print(point2)

class User {
var age: Int
init(age: Int) {
self.age = age
}
}

var user1 = User(age: 27)

var user2 = user1

user2.age = 30

print(user1.age)
print(user2.age)

# Static properties and methods


Static are those variables or func whose are shared among all the instances or objects of a
class/struct. When we define any variable or func “static, it gets attached to a class/struct rather
than an object. The memory for the static variable will be allocated during the loading time.

struct JamesFan {
static var favoriteSong = "Kobita"

var name: String


var age: Int

static func fanFavoriteSong() {


print("James all fan love \(favoriteSong)")
}
}

let fan = JamesFan(name: "Akib", age: 25)

print(JamesFan.favoriteSong)
print(fan.name)
print(JamesFan.fanFavoriteSong())

print(fan.favoriteSong) this will get an error! “Static” member can’t be used on instance of type
‘JamesFan’

# How to access static properties within class/struct


There are two ways to access static properties/methods from non-static properties/methods.
1.
class MyClass {
static let staticProperty = 10
func method() {
print(MyClass.staticProperty)
//In swift5
print(Self.staticProperty)
}
}
2.
class MyClass {
static let staticProperty = 10

static func staticMethod() {


print(staticProperty)
}
}
MyClass.staticMethod()

# Class func

Classes can use the “class” keyword instead to allow subclasses to override the superclass’s
implementation of that method.
You will have understood, unlike static methods, it is possible here to override the method
marked class func, very convenient in the case of inheritance.

class MyClass {
class func doIt() {
print("I'm \(self)")
}
}

class MySubClass: MyClass {


override class func doIt() {
print("I'm a subclass of \(self)")
}
}

MyClass.doIt()
MySubClass.doIt()

# Final keyword into the class


By adding the keyword final in front the method/class name, we prevent the class/method from
being overridden/inherited.

#How can we call the base method without creating an instance?


Method should be “Static method”

# How we achieve inheritance in struct


protocol Vehicle {
var model: String { get set }
var color: String { get set }
}

//Common Implementation using protocol extension


extension Vehicle {

static func parseVehicleFields(jsonDict: [String:Any]) -> (String, String) {


let model = jsonDict["model"] as! String
let color = jsonDict["color"] as! String
return (model, color)
}
}

struct Car : Vehicle {


var model:String
var color:String
let horsepower: Double
let license_plate: String
init(jsonDict: [String:Any]) {
(model, color) = Car.parseVehicleFields(jsonDict: jsonDict)
horsepower = jsonDict["horsepower"] as! Double
license_plate = jsonDict["license_plate"] as! String
}
}

# Overriding & Overloading

Function overloading:
In Swift, if two or more functions are defined using the same name with a different set of
parameters(different types of parameters, different number of parameters or both), this is known
as the Function Overloading. These functions are known as the Overloaded functions.

// same function name different arguments


func test() { ... }
func test(value: Int) -> Int{ ... }
func test(value: String) -> String{ ... }
func test(value1: Int, value2: Double) -> Double{ ... }
Function Overriding:
In swift, Overriding is the process in which a subclass is fully responsible to change or
re-implement the instance method,
instance property and type property which is defined in parent class or superclass.

class Vehicle {

func displayInfo(){
...
}
}

class Car: Vehicle {

// override method
override func displayInfo() {
...
}
}

Overriding Properties:

class OldStudent {
var studentFee = 800
var Fee: String {
return "The Old Fee is \(studentFee)"
}
}

class NewStudent: OldStudent {


var amount = 1000
override var Fee: String {
return super.Fee + ", the override New Fee is \(amount)"
}
}

let newstudent = NewStudent()


newstudent.studentFee = 800
newstudent.amount = 1000
print("\(newstudent.Fee)")

You might also like