[Swift] study notes (8)-class and structure, swift Study Notes
1. Definition of classes and struct
<Pre name = "code" class = "html"> struct Resolution {var width = 0 var height = 0} class VideoMode {var resolution = Resolution () var interlaced = false var frameRate = 0.0 var name: String? // Optional String type name}
Class: keyword class, struct: struct
2. Create instances of classes and struct (Instances in other languages are called objects .)
let someResolution = Resolution()let someVideoMode = VideoMode()
Isn't that easy? Why isn't there a keyword new .... It is the same as assigning a function to a variable. Yes. That's simple. No keyword is required.
3. the struct has an Attribute-by-attribute constructor.
let vga = Resolution(width:640, height: 480)
4. Use a. Dot to access attributes, including access and assignment.
someVideoMode.resolution.width = 1280
5. struct is a value type, and class is a reference type.
6. The constant operator of the class is equivalent to (=), not equal (! =) Determine whether two variables or constants reference the same instance
7. the pointer does not need to be specified (*) to indicate that it is a memory address reference and does not need to be accessed by specific symbols (->, it is used in the same way as the access attribute (.) point.
8. Differences between classes and struct
Classes and struct in Swift have many things in common. The common cause is:
- Define attributes for storing values
- Define a method to provide functions
- Define ancillary scripts for access value
- The definition constructor is used to generate the initialization value.
- Add features implemented by default through extension
- Complies with the protocol to provide standard functions for a certain type
Compared with struct, classes have the following additional functions:
- Inheritance allows one class to inherit the features of another class
- Type conversion allows you to check and interpret the type of a class instance at runtime
- The deconstruct allows a class instance to release any resources it is allocated.
- The reference count allows multiple references to a class.
9. Selection of classes and struct
According to general rules, consider constructing struct when one or more of the following conditions are met:
- Struct is mainly used to encapsulate a small amount of related simple data values.
- It is expected that the encapsulated data will be copied rather than referenced when a struct instance is assigned or transferred.
- Any value type attribute stored in the struct will also be copied rather than referenced.
- Struct does not need to inherit another existing type attribute or behavior.
Appropriate struct candidates include:
- The ry size encapsulates
width
Attributes andheight
Attribute, both of which areDouble
Type.
- Encapsulate a path within a certain range
start
Attributes andlength
Attribute, both of which areInt
Type.
- A point in the 3D coordinate system, encapsulated
x
,y
Andz
Attribute, all of which areDouble
Type.
In all other cases, define a class, generate an instance of it, and manage and transmit it through reference. In practice, this means that most of the custom data structures should be classes rather than struct.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.