Introduction to MVC Architecture
The basic concept
- M: Model, data required by view class, such as tabular data.
- V: View, the class that builds THE UI, such as: button, label, imageView, etc.
- C: Controller, which connects the view class to the model class and makes the data appear on the screen. For example, the controller “fills” the data required by the table into the table view to display the table view.
Communication mode
MVC communication mode is shown in the figure below:
As can be seen from the figure above:
- The Controller has direct access to the Model
- The Controller can access the View directly
- View and Model cannot access each other
1. Communication between View and Controller
Target-action mechanism
: Standardizes anonymous communication, user interaction with view trigger controller method. For example: click the button to realize the view plane jump, network request, refresh UI and so on.Delegate mechanism
: There are more complex generic UI components, such as oneUIScrollView
Slide to the bottom, need to tellController
And ask, can I keep scrolling?DataSource mechanism
: list view (tableView
) cannot process data, requiresController
To help process the data. For example, the data is stored in the Model, and the Controller accesses the Model and pulls out the data to display in the tableView.
2. Communication between Model and Controller
There are two modes of radio-like mechanics:
- Notification
- Key Value Observe (KVO)
Structure (struct)
A struct in C is just some type that stores a little bit of data, nothing important. Structs and classes are basically the same in Swift. They can both have methods and variables.
But there are two main differences:
struct
There’s no inheritance, so there’s moreclass
Some simplestruct
Is the value type, andclass
Is a reference type
Value type: It is copied whenever we pass it as an argument to a function, into an array, or assign a value to another variable. Reference types: They are stored in the heap, retain a pointer to them, and when they are passed, objects are not passed around, only Pointers are passed. We could have a lot of code, all having Pointers to the same object.Copy the code
Lazy loading
Means it is not initialized until it is needed.
The operator
1. for… in
// Half-open interval operatorfor index in0.. <[Any].count { //doSomething} // Close interval operatorfor index in0... [Any].count { //doSomething} // Both of the above can be writtenfor index in [Any].indices { // do something }
Copy the code
Indices: Index to subscript a collection in ascending order. It returns CountableRange
2. The ternary operator
a > b ? a : b
Copy the code
3. Compare operators
A == b = a! = b is not equal to a > b is greater than a < b is less than a >= B is greater than or equal to a <= b is less than or equal toCopy the code
Note: Swift also provides identity (===) and non-identity (! ==) to determine whether two objects reference the same object instance.
4. The control flow
All comparison operations return a Boolean indicating whether the expression is true or not (bool) :
if a == b {
// do something
} else {
// do something
}
Copy the code
Type (static) Method
static var identifierFactory = 0
static func getUniqueIdentifier() -> Int {
identifierFactory += 1
return identifierFactory
}
Copy the code
Property Observer
didSet
var flipCount: Int = 0 {
didSet {
flipCountLabel.text = "Flips: \(flipCount)"}}Copy the code
summary
So that’s basically what we learned in the first class, but it’s important to note that there’s a distinction between struct and class.
Array, Int, Dictionary, and so on are structures that are copied when passed.
Optional:
- Have a value
- Nil
Special care should be taken, otherwise it is easy to crash the program.