classes and structs :
Definition syntax:
class someclass{ // Define the member of the class var "" var - var job:string? // Optional can be empty
}struct somestruct{ // define member of struct " man " }// usually an instance of a class is called an object // instantiation class and struct let SomeClass = = SomeStruct ()
In the swift language, classes and structs have many similarities. They can all:
Defining properties to store values
Define methods to provide functionality
Define initializers to set their initialization state
Can be extended, with features other than the default implementation
Compliance protocol to provide a specific standard function
Of course there are differences, structs do not have the ability to inherit the parent class or other class attributes, the struct is a value type, and the class is a reference type.
A class is a reference type that differs from a value type in that the value of a reference type is not copied when it is assigned to a variable, constant, or passed to a function. Therefore, the reference is made to the existing instance itself, not to its copy. Structs and enumerations are value types
A value type is assigned to a variable, constant, or passed to a function when his value is or is copied. (Swift, all the basic types: integers, floating-point numbers (floating-point), Boolean (Boolean), Strings (string), arrays (array), and dictionaries (dictionary), all of which are value types, And at the bottom are implemented in the form of structs. )
The constructor uses the INIT keyword to declare a constructor, which can have parameters but the constructor does not return a value.
struct Fathernherit { var temp:double init () { 32.0 }} var f = Fathernherit () print ("Thetemp is \ (f.temp) ") // output: The temp is 32.0 fathernherit
Selection of classes and structure bodies
However, struct instances are always pass values, and class instances are always passing references, meaning they apply to different types of tasks. We consider the structure and function of the data according to the needs of the project, and decide whether each data type should be defined as a class or struct.
A common rule is to consider using a struct to define when one or more of the following conditions are met:
The main purpose of this structure is to encapsulate the value of a small amount of relatively simple data.
When an instance of a struct is assigned or passed a value, the encapsulated value is copied instead of referenced.
Any property that is stored by the struct is their own value type, which is a copy rather than a reference.
Structs do not need to inherit other types of properties or behaviors.
Examples of suitable use of structural bodies:
Describes the dimensions of a geometric shape, encapsulating the width and height of a double type and two properties.
Describes a range that encapsulates the start property of an int type and the length property of an int type.
Used to describe a point in a three-dimensional coordinate system that encapsulates the X, Y, and Z Three properties of a double type.
All other cases use the class definition and create instances of the class to be managed and passed by reference. In fact, most data structures should use classes rather than structs.
There are two ways to jump from one interface to another
1. Program code Implementation
Let Scondviewcontroller = Self.storyboard?. Instantiateviewcontrollerwithidentifier ("Scondviewcontroller") as! Comiclsitviewcontrollerscondviewcontroller.artistname = Label.text!self.navigationcontroller?. Pushviewcontroller (Scondviewcontroller,animated:true)
2.storyboard implementations
First click viewcontroller->editor->embed in---Navigation Controller The next step is to correlate the interface as.
Swift syntax knowledge (class struct, constructor, value, and reference)