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 subscript for accessing values through subscript syntax
? Defines the initialization device 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
For more information, see attributes, methods, subscripts, initial processes, extensions, and protocols.
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 uninitialized device allows a class instance to release any resources it is allocated.
? The reference count allows multiple references to a class.
For more information, see inheritance, type conversion, initialization, and automatic reference count.
Note:
The struct is always transmitted in the Code by copying it. Therefore, do not use reference count.
Definition
Classes and struct have similar definitions. We use the keywords class and struct to represent classes and struct respectively, and define their specific content in a pair of braces:
class SomeClass { // class definition goes here}struct SomeStructure { // structure definition goes here}
Note:
Every time you define a new class or struct, you actually effectively define a new SWIFT type. Therefore, use uppercamelcase (such as someclass and somestructure) to conform to the standard capital naming style of SWIFT (such as string, Int, and bool ). Instead, use lowercamelcase to name attributes and methods (such as framerate and incrementcount) to distinguish them from classes.
The following is an example of defining struct and definition classes:
struct Resolution { var width = 0 var heigth = 0}class VideoMode { var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String?}
In the preceding example, we define a structure named resolution to describe the pixel resolution of a display. This struct contains two storage attributes named width and height. A storage attribute is a constant or variable bound to and stored in a class or struct. When these two attributes are initialized to an integer 0, they are inferred to be of the int type.
In the above example, we also define a class named videomode to describe a specific mode of a video display. This class contains four storage attribute variables. The first is the resolution. It is initialized as an instance of a new resolution struct and has a resolution attribute type. The new videomode instance also initializes the other three attributes, which are the intereceived whose initial value is false (meaning "non-interlaced video, the framerate value of the initial playback frame rate of 0.0 and the value of the optional string name. The name attribute is automatically assigned a default value nil, indicating "no name value" because it is an optional type.
Class and struct instance
The definition of the Resolution struct and videomode class only describes what is resolution and videomode. They do not describe a specific resolution or video mode ). To describe a specific resolution or video mode, we need to generate an instance of them.
The syntax for generating struct and class instances is very similar:
let someResolution = Resolution()let someVideoMode = VideoMode()
Both struct and class use the initializer syntax to generate new instances. The simplest form of the initialization syntax is to follow an empty arc after the type name of the struct or class, such as resolution () or videomode (). The class or struct instance created in this way will be initialized to the default value. The constructor chapter will discuss the initialization of classes and struct in more detail.
Attribute access
By using DOT syntax, you can access the properties contained in the instance. The syntax rule is that the Instance name is followed by the attribute name, and the two are connected by the dot:
Println ("the width of someresolutionis \ (someresolution. width)") // output "the width ofsomeresolution is 0"
In the preceding example, someresolution. Width references the width attribute of someresolution and returns the initial value of width 0.
You can also access the width attribute of the Resolution attribute in videomode:
Println ("the width of somevideomode is \ (somevideomode. Resolution. width)") // output "the width ofsomevideomode is 0"
You can also use the dot syntax to assign values to attribute variables:
Somevideomode. Resolution. width = 12880 println ("the width of somevideomode isnow \ (somevideomode. Resolution. width)") // output "the width ofsomevideomode is now 1280"
Note:
Unlike the objective-C language, swift allows you to directly set sub-attributes of struct attributes. In the last example, the sub-attribute width of the Resolution attribute in somevideomode is directly set. The preceding operation does not need to set the resolution attribute.
Initialize the object type one by one.
//Memberwise Initializers for structureTypes
All Schemas have an automatically generated member initialize one by one, which is used to initialize the attributes of members in the new struct instance. The initial values of each attribute in the new instance can be passed to the members one by one through the attribute name:
Unlike struct, the class instance does not have default members to initialize one by one. The constructor will be discussed in more detail in the constructor.
Swift -- Comparison of classes and struct