To be able to develop your app early, so you re-learn swift.
Unlike other programming languages, Swift does not require you to create separate excuses and implementation files for classes and structs, and the system automatically generates external interfaces for other code.
The common denominator of classes and structs:
- Defining properties for storing values
- Define methods to provide functionality
- Defining satellite scripts for accessing values
- Defining constructors for initializing values
Compared to structs, classes have the following additional functions:
- Inheritance allows one class to inherit the characteristics of another class
1. Constructors
A constructor is a method that creates an instance of a concrete type. The simplest constructor is a method that does not have any argument instances, writing init.
2. Custom Initialization
You can customize the initialization process and optional property types based on the input parameters, or modify the static properties at initialization time. This section will be described in detail.
Initialize parameters
You can provide a subset of parameters when the constructor is defined, defining the type and name of the variable during the custom initialization process.
Initialization parameters have the same function as functions or method parameters
3. Optional type
If you store properties using a custom type that logically allows values to be null-or their values are not initialized in the constructor, or they are allowed to be empty. You can define an attribute of an optional type. The optional type attribute is an auto-initialize value of nil, which indicates that this property is intentionally set to "null" in the constructor.
4. Initialization and inheritance of classes
All storage properties of a class-including those inherited from the parent class-must be set at initialization time.
5. Value types and reference types
A value type ? is when you assign a value or pass a parameter to a function, create a copy and pass the copy past so that the original data is not affected during the function call.
reference type ? In passing parameters, the data itself is passed over so that the original data is affected during the function call.
The difference between Swift class and struct