An introduction to iOS development Architecture design and team collaboration for MVVM

Source: Internet
Author: User

Today to write this blog is to achieve a role, want to exchange ideas with you, learn from each other, there are shortcomings in the blog also hope that we criticize. The content of this blog is based on the style of the past blog, but also dry-based, occasionally pull the salty egg (haha ~ not good work and began to publish blog ~).

Due to my limited project experience and limited understanding of the architecture design, my personal understanding of MVVM is mostly borrowed from the previous MVC web framework-used by the thinkphp framework in school, and the SSH framework, which are MVC's architectural patterns. Today's MVVM is very similar to the traditional MVC, and it can be said to be a sibling, a family.

Speaking of architectural design and teamwork, the development of the app is still more important. Even as a professional brick-and-tile, if you put it in the end of the brick where? Not only the code has the framework, the other things are framed, such as bridges and so on God horse ~ here will not pull out. A good project framework can improve team collaboration efficiency, but also can reduce the code, reasonable division of labor and system architecture design is indispensable.

As far as team collaboration is not just with SVN or Git version control tools, for how to use SVN in iOS development, refer to the previous blog (iOS development version Control (SVN)). A team can work efficiently, I feel that communication is the most important, everyone in the team is more pleasant, and communication is not any obstacle, communication in the team is the most important. As for how SVN is used, it's not a thing!

All right, today, with a demo I wrote about the architecture design and teamwork in iOS development, today's salted eggs come here first and cut into today's topic. In order to write today's blog I took a little time to do a project, the project backend interface with the Sina Weibo API to test, and later in this article will follow the GitHub share link. Ok~ said the tall on some is, the benevolent see, exchange ideas, learn together.

A drink of MVVM

In this, do not dwell on what is MVC, God horse is MVVM, Google on Baidu on a grab a lot, here simply put on a mouth. The following demo uses the schema mode of MVVM.

The model layer is indispensable, we have to have something to act as a DTO (data transfer object), of course, with a dictionary is also possible, programming, to be flexible. The model layer is a relatively thin layer, if you learn Java's small partners, JavaBean should not be unfamiliar with it.

The ViewModel layer, the binder for the view and model layers, is an excellent place to put user input validation logic, view display logic, initiating network requests, and a variety of other code. To put it bluntly, the original Viewcontroller layer of business logic and page logic and so stripped out to the ViewModel layer.

The view layer is the Viewcontroller layer, and his task is to get the data from the ViewModel layer and display it.

The above for MVVM on the first simple to say, good understanding and application, but also to combat.

Ii. discussion on whether to use storyboard in engineering

From the Internet often see that it is not recommended to use storyboard or xib, the recommendation of pure code handwriting. Personally think that this view and Apple design Storyboard contrary to the original intention, in the project I have done is mainly storyboard, xib supplemented, and then the code to integrate each storyboard.

To take a storyboard advantage of the example is OK, to add constraints to the control, if the use of storyboard to accomplish that is minutes and seconds, and the code to add constraints is how disgusting, pure code writing will spend a lot of time on the writing UI, and the technical content is relatively low, The man thought there was no need. The small partners responsible for UI development in team work just no one is responsible for a storyboard, each development, with SVN submit the following tick (such as) to remove, so use storyboard is no problem. Then the integration with the code is OK. If you have added a new resource file to your project, you will need to submit it with the project setting file if you submit it with Xcode's own SVN.

Third, actual combat MVVM (group created with Xcode is a virtual folder, it is recommended to create a physical folder for ease of maintenance, and then manually introduced)

1. Below is an example of the MVVM schema pattern, the following is the first level of the project directory below, each layer of interaction is implemented in the form of block

Project Catalog Description:

1.Request: The class that stores the network request under the folder, the following will give a specific implementation

2.Config: Is the project configuration file

3.Resource: Is the project resource file, the following picture resources and storyboard file resources

4.Tools is: Tool file class, store tool class, such as data regular matching.

5.Vender: Storage of third-party class libraries

6.Model: That's not much to say.

7.ViewController: Store the Viewcontroller class resource file, which is the view layer

8.ViewModel: Store various business logic and network requests

2. Detailed request:request is responsible for the network request for things, as follows:

Netrequestclass is the code that holds the network request, this project uses the AF, because this project is only a demo, therefore only then encapsulates the monitoring network state, the GET request, the POST request method, according to the actual need, may also encapsulate uploads the download and so on the class method.

The code in NETREQUESTCLASS.H is as follows:

View Code

The code in NETREQUESTCLASS.M is as follows:

View Code

3. config: Create pch file, and Config.h file

    

PCH file introduces a common header file, as follows:

View Code

Config.h is a variety of macro definitions and various enumeration types and block types, the code is as follows:

View Code

4. Detailed resource file resource, structure such as:

    

Image in the storage of various pictures (3X,2X, etc.), interfacebuider inside is to put some xib and storyboard files, each responsible UI developer responsible for a storyboard

5. Detailed model: The project is to request public microblogging interface we need to be on the page real user's avatar, user name, release date, blog, already implicit user ID and Weibo ID, file directory structure as follows:

  

The contents of the Publicmodel are as follows:

View Code

6. Detailed ViewModel layer, this layer is the most important layer, the following is the detail of this layer, Viewmodeclass is the parent class of all ViewMode, which stores the common part

    

The contents of the ViewModelClass.h are as follows:

View Code

The contents of the VIEWMODELCLASS.M are as follows:

View Code

The contents of the PublicWeiboViewModel.h are as follows:

View Code

The contents of the PUBLICWEIBOVIEWMODEL.M are as follows:

View Code

The directory structure of the 7.ViewController layer is as follows:

  

The above code will not stick to each other (the main hand by command + C Press tired), the following links will have the source code

The structure in 8.storybord is as follows:

  

The final effect of the run:

  

   

9. The complete directory structure, the business logic between the pages, and the request data of the network are placed in the ViewModel layer, of course, this is not absolute, to be flexible grasp. I personally like programming, because the programming flexibility will be very fun.

  

10. The blog is almost over, here is a summary of the use of SVN more frequent and more important command bar (of course, I usually mainly with Xcode with the SVN visual management ~)

(1), update Local Code command

1.1 SVN up update version

1.2 SVN Info View current version information

(2), the code of the submission

2.1 SVN Info View current local version information

2.2 SVN up update to latest version information

2.3 SVN St View file status m-Modify D-delete A-add U-update?-unknown state--warning C-conflict

2.4 svn Add FileName: What if it appears? Generally is the problem when adding files, after the file svn add filename, the status of the file will be changed to a

2.5 svn del fileName: if it appears! This is usually a warning when you delete a file. After executing the SVN del filename command, the file's status is changed to D

2.6 svn ci-m "reason for submission"

    

At the end of the article attached to the demo: GITHUB:HTTPS://GITHUB.COM/LIZELU/MVVM, what questions to give me a message, or in Q I can all ~

For RAC, please refer to: MVVM under iOS development Reactivecocoa (dry share)

An introduction to iOS development Architecture design and team collaboration for MVVM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.