|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "iOS Development Notes" |
| 4 | +author: "Yalun Hu" |
| 5 | +categories: journal |
| 6 | +tags: [Blog, iOS, Swift] |
| 7 | + |
| 8 | +image: mountains.jpg |
| 9 | +--- |
| 10 | + |
| 11 | +## 如何快速查看文档 |
| 12 | + |
| 13 | +在Xcode中按住Option然后点击对应的关键字或builtin types,便能快速跳转到对应的文档下。 |
| 14 | + |
| 15 | +## 类和结构体对比 |
| 16 | + |
| 17 | +Structures and classes in Swift have many things in common. Both can: |
| 18 | + |
| 19 | +- Define properties to store values |
| 20 | +- Define methods to provide functionality |
| 21 | +- Define subscripts to provide access to their values using subscript syntax |
| 22 | +- Define initializers to set up their initial state |
| 23 | +- Be extended to expand their functionality beyond a default implementation |
| 24 | +- Conform to protocols to provide standard functionality of a certain kind |
| 25 | + |
| 26 | +For more information, see [Properties](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties), [Methods](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/methods), [Subscripts](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/subscripts), [Initialization](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization), [Extensions](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/extensions), and [Protocols](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols). |
| 27 | + |
| 28 | +Classes have additional capabilities that structures don’t have: |
| 29 | + |
| 30 | +- Inheritance enables one class to inherit the characteristics of another. |
| 31 | +- Type casting enables you to check and interpret the type of a class instance at runtime. |
| 32 | +- Deinitializers enable an instance of a class to free up any resources it has assigned. |
| 33 | +- Reference counting allows more than one reference to a class instance. |
| 34 | + |
| 35 | +For more information, see [Inheritance](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/inheritance), [Type Casting](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/typecasting), [Deinitialization](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/deinitialization), and [Automatic Reference Counting](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/automaticreferencecounting). |
| 36 | + |
| 37 | +## 协议[Protocols] |
| 38 | + |
| 39 | +A *protocol* defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be *adopted* by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to *conform* to that protocol. |
| 40 | + |
| 41 | +Multiple protocols can be listed, and are separated by commas: |
| 42 | + |
| 43 | +```swift |
| 44 | +struct SomeStructure: FirstProtocol, AnotherProtocol { |
| 45 | + // structure definition goes here |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +If a class has a superclass, list the superclass name before any protocols it adopts, followed by a comma: |
| 50 | + |
| 51 | +```swift |
| 52 | +class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol { |
| 53 | + // class definition goes here |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## MVVM设计模式 |
| 58 | + |
| 59 | +Model-View-ViewModel is a design paradigm. |
| 60 | + |
| 61 | +It must be adhered to for SwiftUI to work. |
| 62 | + |
| 63 | +### Model |
| 64 | + |
| 65 | +- UI Independent |
| 66 | +- Data + Logic |
| 67 | + |
| 68 | +### View |
| 69 | + |
| 70 | +- The reflection of the Model |
| 71 | +- Stateless |
| 72 | +- Declared(只有“var body”决定了view是如何绘制的) |
| 73 | +- Reactive (Always reacting efficiently to the change on the model) |
| 74 | +- Automatically observes publications from ViewModel( or subscribe what they interested at from the ViewModel) .Pulls data and rebuild itself. |
| 75 | + |
| 76 | +### ViewModel |
| 77 | + |
| 78 | +- Binds View to Model(so the change on the model cause the view to react and get rebuilt) |
| 79 | +- Interpreter (between Model and View). Help View code stay clean and neat. |
| 80 | +- Gatekeeper. |
| 81 | +- Constantly noticing changes in the Model |
| 82 | +- Publish a message globally once any change in the Model is noticed (avoid have any connection to any of the View that using it to access the Model) |
| 83 | +- Processing User Intent(Change the Model based on the events occurs in the View) |
| 84 | + |
| 85 | +### Rules in MVVM |
| 86 | + |
| 87 | +- The View must always get data from the Model by asking it from the ViewModel. |
| 88 | +- The ViewModel never stores the data for the Model in side of itself. |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
0 commit comments