Professional Documents
Culture Documents
# 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
print(taylor)
print(taylorCopy)
# 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.
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 {
func describe() {
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() {
}
}
await print(actor1.score)
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
Output:
Area of Wall 1: 90.3
Area of Wall 2: 53.55
#Initializer Overloading
class Person {
var age: Int
# convenience Initializer
class University {
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
}
}
struct Coordinates {
var lat: Double
var lng: Double
}
print(point1)
print(point2)
class User {
var age: Int
init(age: Int) {
self.age = age
}
}
user2.age = 30
print(user1.age)
print(user2.age)
struct JamesFan {
static var favoriteSong = "Kobita"
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’
# 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)")
}
}
MyClass.doIt()
MySubClass.doIt()
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.
class Vehicle {
func displayInfo(){
...
}
}
// override method
override func displayInfo() {
...
}
}
Overriding Properties:
class OldStudent {
var studentFee = 800
var Fee: String {
return "The Old Fee is \(studentFee)"
}
}