1. Definitions of classes and structures
<pre name= "code" class= "HTML" >struct Resolution { var width = 0 var height = 0}class Videomode { var res Olution = Resolution () var interlaced = False var framerate = 0.0 var name:string? The value is an optional string type name}
Class: Keyword class, struct: struct
2. Create instances of classes and structs (other languages are called instances as objects.) )
Let someresolution = Resolution () Let Somevideomode = Videomode ()
Gee, very simple ah, how not the keyword new .... and assigning a function to a variable is the same. Yes. It's so simple. No keywords required.
3, the structure has a property-by-attribute constructor, class wood has.
Let VGA = Resolution (width:640, height:480)
4, use. Points to access properties, including access and assignment.
SomeVideoMode.resolution.width = 1280
5, the struct is a value type, and the class is a reference type.
6. The identity operator of the class is equivalent to (= = =), not equivalent to (!==) determining whether two variables or constants refer to the same instance
7, the pointer does not need a specific (*) to indicate is a memory address reference, and do not need a specific symbol (-) to access, it is the same as the Access property (.) Point.
8. Differences between classes and structures
Classes and structs in Swift have a lot in common. The Common place is:
- Defining properties for storing values
- Define methods to provide functionality
- Defining satellite scripts for accessing values
- Defining constructors for generating initialization values
- Extended to add functionality to the default implementation
- Compliance with protocols to provide standard functionality for a class
Compared to structs, classes have the following additional functions:
- Inheritance allows one class to inherit the characteristics of another class
- Type conversions allow you to check and interpret the type of a class instance at run time
- The destructor allows an instance of a class to release any resources it has been assigned
- Reference count allows multiple references to a class
9. Selection of classes and structures
As a general guideline, consider building a struct when one or more of the following conditions are met:
- The main purpose of the structure is to encapsulate a small number of related simple data values.
- It is reasonable to expect that when an instance of a struct is assigned or passed, the encapsulated data will be copied instead of referenced.
- Any value type attribute stored in the struct will also be copied, not referenced.
- Structs do not need to inherit the properties or behaviors of another existing type.
Suitable structural candidates include:
- The size of the geometry, encapsulating an
width attribute and a height property, both Double types.
- A path within a range that encapsulates a
start property and length attribute, both of which are Int types.
- In a three-dimensional coordinate system, a point, package
x , y and z attribute, all of them are Double types.
In all other cases, define a class, generate an instance of it, and manage and pass it by reference. In practice, this means that the vast majority of custom data constructs should be classes, not structs.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Swift" Learning Notes (eight)--classes and structs