This is the first day of my participation in Gwen Challenge

Written in the beginning

My swift15-day learning plan came to class; Today’s main topic: similarities and Differences between classes and structs

Classes and structures are very similar in use, and you can use them to create new data types with properties and methods. Classes have inheritance, which is a very useful, important and complex feature that allows us to create a new class based on an existing class. However, we must use it in iOS App development, but overuse can lead to bloated and inefficient code and performance; Keeping code simple is always a good programmer’s skill.

“Any fool can write code that a computer can understand, But good programmers write code that humans can understand. “–Martin Fowler

There are five differences between classes and structures

1. The member initializes the constructor

Classes have default no-parameter constructors, structs have no default no-parameter constructors, and can only be declared as parameterized constructors.

class Dog {
    var name: String
    var breed: String

    init(name: String.breed: String) {
        self.name = name
        self.breed = breed
    }
}

let myDog = Dog(name: "Little Lion", breed: "Poodle")


struct Dog {
    var name: String
    var breed: String
}

Copy the code

2. The inheritance

Classes can inherit from classes, interfaces, and structs can only inherit from interfaces, but cannot be inherited.

class Dog {
    var name: String
    var breed: String / / varieties

    init(name: String.breed: String) {
        self.name = name
        self.breed = breed
    }
}
Copy the code
class Poodle: Dog {
    init(name: String) {
        super.init(name: name, breed: "Poodle")}}Copy the code

Subclasses can override methods of their parent class

class Dog {
    func makeNoise(a) {
        print("Woof woof!)}}class Poodle: Dog {
    override func makeNoise(a) {
        print("Yip!")}}Copy the code

Final

final class Dog {
    var name: String
    var breed: String

    init(name: String.breed: String) {
        self.name = name
        self.breed = breed
    }
}
Copy the code

When you don’t want someone overriding a method to change something you don’t want by inheriting from your class, you can use the final keyword to block other class inheritance

3. Copy objects

When you copy a struct, you get two different data objects, and changing any of them doesn’t affect each other; When you copy a class, two objects refer to the same object, modify one, and the other changes as well;

When passed as a parameter, class variables are passed by address, while struct variables are passed by value.

class Singer {
    var name = "Mayday"
}

var singer = Singer(a)print(singer.name) / / a mayday

var singerCopy = singer
singerCopy.name = "Jay Chou"

print(singer.name) / / jay Chou

Copy the code

If you change class Singer to struct Singer you get two different singers

4. destructor (deinit)

Classes have destructors, structs don’t; The destructor is automatically called one step before the instance is released. The class does not actively call its own destructor. The instance is released only when the destructor is called. Class instances are garbage collected to ensure that memory is reclaimed, and struct variables are automatically allocated after being used up.

deinit {
	NSNotificationCenter.defaultCenter().removeObserver(self)}Copy the code

5. Mutability

Fifth difference: Class and Contant also handle constants differently; If you have a constant struct, the variable defined in it cannot be changed because it is itself a constant value; But if you are a constant class, the value can be changed; You don’t need to use the mutating keyword to modify;

So classes, whether created normally or defined as constant, can change the value of a property

struct Singer {
    var name = "Jay Chou"
}

let s = Singer()
s.name = "Mayday" / / an error
print(s.name)
Copy the code

Reference:

www.hackingwithswift.com/100/10