I wrote this article today to serve as a starting point for discussion and exchange of ideas with you all, so that we can learn from each other. If there are any deficiencies in this blog, I hope you can criticize and correct me. The content of this article follows the style of previous blogs, which is mainly practical, with occasional nonsense (haha~ I’m not working hard and I’m starting to blog again~). Due to my limited project experience and limited understanding of architectural design, my personal understanding of MVVM is mainly based on the MVC Web framework I used before. When I was in school, I used the ThinkPHP framework and the SSH framework, both of which are MVC architectural models. Today, MVVM and traditional MVC are extremely similar, and they can be said to be brothers, or family members. Speaking of architecture design and team collaboration, this is quite important for App development. Even as a professional brick mover, the premise is where do you put the bricks after moving them? Not only Code has a framework, but other things also have frameworks, such as bridges and so on. I won't go into details here. A good engineering framework can not only improve the efficiency of team collaboration, but also reduce the redundancy and coupling of code. Reasonable division of labor and system architecture design are indispensable. As for team collaboration, it is not enough to just have version control tools such as SVN or Git. As for how to use SVN in iOS development, please refer to the previous blog (iOS Development Version Control (SVN)). A team can work efficiently. I think communication is the most important thing. Everyone in the team is relatively friendly, and there are no obstacles in communication (but there are always a few people in some teams who don't get along). Communication is the most important thing in a team. As for how to use SVN, that's not a problem! Okay, today I will use a demo I wrote to talk about the architectural design and team collaboration in iOS development. That’s all for today, let’s get into today’s topic. In order to write today's blog, I spent some time to build a project. The backend interface of this project was tested using the Sina Weibo API. The GitHub sharing link will be posted later in this article. OK~ To put it in a more high-sounding way, everyone has their own opinions, so let's exchange ideas and learn together. 1. A little MVVM I won't go into details about what MVC is, and what MVVM is. You can find a lot of information by searching on Baidu, so I'll just briefly mention it here. The following demo uses the MVVM architecture pattern. The Model layer is indispensable. We need something to act as a DTO (Data Transfer Object). Of course, a dictionary is also possible. Programming requires flexibility. The Model layer is a relatively thin layer. If you have learned Java, you should be familiar with JavaBean. The ViewModel layer is the glue between the View and Model layers. It is an excellent place to place user input validation logic, view display logic, network request initiation and other various codes. To put it simply, it is to separate the business logic and page logic of the original ViewController layer and put it in the ViewModel layer. The View layer is the ViewController layer. Its task is to obtain data from the ViewModel layer and then display it. The above is just a brief introduction to MVVM. If you want to understand and apply it well, you still need to practice it. 2. Discussion on whether to use StoryBoard in the project I often see on the Internet that it is not recommended to use StoryBoard or Xib, and it is recommended to write it in pure code. I personally think that this view is contrary to the original intention of Apple to design StoryBoard. In the projects I have done, StoryBoard is the main one, and Xib is supplemented, and then each StoryBoard is integrated with code. Let me give you an example of the benefits of using Storyboard. If you use Storyboard to add constraints to controls, it takes only a few seconds. However, adding constraints with code is disgusting. If you write it with pure code, you will spend a lot of time writing UI, and the technical content is relatively low. I personally think it is unnecessary. In teamwork, the partners responsible for UI development only need to have no one responsible for a Storyboard, and each develop their own. When submitting with SVN, just remove the check mark below (as shown below), and there will be no problem using Storyboard. Then integrate with code. If you add new resource files to your project, you need to submit the Project Setting file together with it when submitting with XCode's built-in SVN. 3. Practical MVVM (Group created with Xcode is a virtual folder. For ease of maintenance, it is recommended to create a physical folder and then introduce it manually) 1. Let's experience the MVVM architecture pattern through an example. The following is the first-level directory of the project. The interaction between each layer is implemented in the form of Block. Project Catalog Description: Request: The class that stores network requests in the folder. The specific implementation is given below. Config: is the configuration file of the project Resource: It is the resource file of the project, including image resources and Storyboard file resources. Tools is: tool file class, which stores tool classes, such as data regular matching, etc. Vender: stores third-party libraries Model: I won’t say much about this ViewController: stores ViewController class resource files, that is, the View layer ViewModel: stores various business logic and network requests 2. Detailed explanation of Request: Request is responsible for network requests, as follows: NetRequestClass is the code for storing network requests. This project uses AF. Because this project is just a Demo, it only encapsulates the monitoring network status, GET request, and POST request methods. According to actual needs, it can also encapsulate upload and download methods. The code in NetRequestClass.h is as follows:
The code in NetRequestClass.m is as follows:
3. Detailed explanation of Config: creating pch files and Config.h files The pch file introduces commonly used header files, the contents are as follows:
Config.h contains various macro definitions, enumeration types, and block types. The code is as follows:
4. Detailed explanation of resource file Resource, the structure is as follows: Image stores various images (3x, 2x, etc.), InterfaceBuilder stores some Xib and Storyboard files, and each developer responsible for UI is responsible for a Storyboard. 5. Detailed explanation of Model: This project uses the public Weibo interface to request. We need to display the user's avatar, username, release date, blog post, implicit user ID and Weibo ID on the page. The file directory structure is as follows: The content of PublicModel is as follows:
6. Detailed explanation of the ViewModel layer. This layer is the most important layer. The following is a detailed screenshot of this layer. ViewModeClass is the parent class of all ViewModes, which stores the common parts The content of ViewModelClass.h is as follows:
The contents of ViewModelClass.m are as follows:
The contents of PublicWeiboViewModel.m are as follows:
7. The directory structure of the ViewController layer is as follows: I won't paste the above codes one by one (mainly because I'm tired of pressing command + C by hand), the link below will have the source code 8. The structure in storybord is as follows: The final result of running: 9. The complete directory structure, business logic between pages, and network request data are placed in the ViewModel layer. Of course, this is not absolute and needs to be handled flexibly. I personally like programming very much because it is fun to be flexible in programming. 10. The article is almost over. Here I will summarize the most frequently used and important commands of SVN (of course, I usually use the SVN visual management that comes with XCode~) (1) Update local code command svn up Update version svn info View the current version information (2) Code submission svn info View the current local version information svn up Update to the latest version information svn st View file status M-modify D-delete A-add U-update?-unknown status!-warning C-conflict svn add fileName: If it appears? Usually it is a problem when adding files. After svn add fileName, the status of the file will change to A svn del fileName: If it appears! It is usually a warning that appears when deleting a file. After executing the svn del fileName command, the status of the file will change to D svn ci -m "Reason for commit" |
<<: iOS upgrades stagnate, app crash rates slowly decrease
>>: Messaging apps will become the next generation of Internet portals
After reading so much World Cup marketing informa...
Apple has assembled a secret team to explore chan...
Snacks have been popular since ancient times. Now...
The key to planning fission activities lies in th...
The advantages of WeChat mini program investment ...
Regardless of whether one has the resources, expe...
Do you still remember the Zhijie S7 jointly devel...
Recently, Anhui Province discovered a large group...
The autumn breeze brings the fragrance of fruits ...
Snapchat has been facing pressure from a number o...
Author: Zhang Yuzhi A recent news (November 24, 2...
In recent days, extreme rainfall has occurred in ...
Review expert: Shi Jun, Ph.D. in Botany "......
As someone who has worked on mobile phone structu...
When doing operations , one of the most important...