Joke Collection Website - Bulletin headlines - Similarities and differences between Swift class and struct
Similarities and differences between Swift class and struct
2. Define the method of providing functions.
3. Defining the subscript operation makes it possible to access the values (subscripts) contained in the instance through the subscript syntax.
4. Define the constructor to generate the initializer.
5. Extensions add the functions realized by default (extensions).
6. Implement protocols to provide standard functions (protocols)
1.class is a reference type; Structure is a value type
2. Classes support inheritance; Structure does not support inheritance.
3.3. The method declared by class does not need to mutate keywords when modifying attributes; Structural requirements
Every member variable in 4.4.class must be initialized, otherwise the compiler will report an error, but struct doesn't need it. The compiler will automatically help us generate the init function and assign a default value to the variable.
5.class supports reference counting (allowing multiple references to a class), but struct does not.
6.class supports type conversion (allowing the types of class instances to be checked and interpreted at runtime), but struct does not.
7.class supports de-initializers (allowing class instances to release any allocated resources), but struct does not.
8. Variables are assigned in different ways (deep copy), such as shallow copy of classes and deep copy of structures. The assignment of class is to pass references, and struct passes values by copying instead of using reference counting.
9. memory management: struct is stored in the stack, class is stored in the heap,
10. Method scheduling mode: the method call of struct is statically bound, while the method call of class is dynamically implemented.
The object of Tip: class is a reference type, and struct is a value type. So the assignment of class is to pass the reference, and pass the value by copying instead of using the reference count.
Class will increase its complexity to support additional functionality. Generally speaking, we prefer to choose struct and enum because they are easier to understand, while class is used when appropriate and necessary. In fact, this means that most custom data types are defined as struct and enum.
Selection of classes and structures
Structure instances are always passed by value, while class instances are always passed by reference. This means that they are suitable for different tasks. When you consider the data structure and function of an engineering project, you need to decide whether each data structure is defined as struct or class.
In general, when one or more of the following conditions are met, the construction structure can be considered:
To sum up, it is a sentence: if you can use struct, don't use class.
Why is struct preferred?
Identity operator
Because class is a reference type, there may be multiple constants and variables referencing the same class instance in the background. (This is not true for structures and enumerations. Because they are value types, when they are assigned to constants, variables or passed to functions, their values are always copied. )
It is helpful if you can determine whether two constants or variables refer to the same class instance. To achieve this goal, Swift has established two identity operators:
- Previous article:What do escort security guards on cargo ships do?
- Next article:Three Slogans of Healthy and Sunshine Sports
- Related articles
- Work plan of the legal affairs unit of the forestry department
- How to promote sanitary napkins
- Experience of safety warning education of "controlling drunk driving, strict discipline and being positive"
- Figure Sketch Series ¡ª¡ª Impression of Peer Biaoge
- How much salt should a person eat every day?
- Four sentences of the seven laws of epidemic disease
- Shijiazhuang volunteers wrote "not a property" on their backs. Why did they write that?
- What is the slogan of standard word?
- What should be written in the traffic safety handwritten newspaper?
- Summary of work after the Spring Festival