This article mainly explains 4 questions
1 Reduce the burden on appDelegate After a long period of study, you finally mastered the iOS method. You found a job as an iOS developer and vowed to start your coding career. Your boss thinks highly of you and tells you that your skills are very tricky, so you should handle this project by yourself. Oh, this means that you will handle the entire project from start to finish, from the software architecture to the page display. Using my half-baked papapa coding, I am determined to encapsulate the code well and write it beautifully (actually I heard from the experts about encapsulation, but I don’t really understand it) The project is coming to an end. The boss tells you that our app will have a function of sharing to Moments in the future. Otherwise, how can we show the awesomeness of our product? Then you heard that Umeng is better (it may be an advertisement). You went to Umeng to read their documentation and they told you to write this in the appdelegate didFinishLaunch method.
A few days later, my boss said we need to collect statistics on my page. You connected to Umeng's statistics and added another line of code in appdelegate didFinishLaunch. The needs are endless. I need bug statistics (fir hud), user reminder rating system (iRate), push (jPush, pigeon push...) Your determination to encapsulate the code perfectly and write it beautifully has long been defeated by your boss's demands. Don't worry, Brother Han will teach you some tricks Have you ever used smart libraries like IQKeyBoardManage and iRate? Daniel's readme wrote this paragraph
In fact, it is not magical, it is just that the expert used the + load method Anyone who studies OC knows that this code will be automatically called when a class is loaded into the runtime library. This is not the realization of automatic calling. Write a class that inherits from NSObject
Similar to positioning, you can also write Modules and services are completely separated But some services such as APNS require LaunchOption, so they can only be written in appdDelegate. But in this way, a lot of code has been removed and only a few fixed ones are left. When you modify appDelegate later, it will feel very clear. 2 ViewController inheritance? Next, we have connected to Umeng Statistics. The most basic thing about Umeng Statistics is the PV of the statistics page. Umeng writes this way. For newbies, we think this is so easy. I opened a certain vc (HomeViewController) Wrote this in the code
Then I may have dozens or even hundreds of pages in a project that need to count pv. I can't write this for every program. We are smart and thought of inheritance Such as MyBaseViewController:UIViewController In this way, we need to do one thing: change all the classes in our project that inherit from UIViewController to inherit from MyBaseViewController. However, do you really think this is a good idea? We have dozens of controllers in a project, so I have to change each controller. This kind of repetitive work is boring and prone to errors. You will miss a class when you copy it. The important thing is that many classes in our project are not directly inherited from UIViewController. Some may be UITableViewController, UICollectionViewContr0ller, UINavigationController, or even the less commonly used UISearchDisPlayController, UIPopoverController, and UIPresentController. Do you suddenly feel that there are so many? This is not a trap. The trap is that in the future, when you become a big shot and hire a little brother, you tell him that all his classes must inherit from the various parent classes I wrote. Newbies always make mistakes without paying attention. Some classes forget to inherit, which makes it very difficult to check later and wastes time. Therefore, this design is unreasonable.
What is this for? Baidu Here is an article from NSHipster blogger Chinese Translation There is also an article explaining runtime. practice By using method intersection, we can intercept the attraction method. Here is the code. This achieves the idea of aspect-oriented programming (AOP) On the code
A copy of the picture code is convenient for viewing We have taken full advantage of black magic to achieve the benefits of aspect-oriented programming The idea comes from here http://casatwy.com/iosying-yong-jia-gou-tan-viewceng-de-zu-zhi-he-diao-yong-fang-an.html Black magic is not poison. Code written in accordance with a specification will not crash. As long as it can help us solve the problem, it is a good thing. Black Magic performance has bottlenecks? You are still worried about bottlenecks even though it has reached the bottom of the runtime? Just use it with peace of mind? If you don't agree, you can use Time Profiel to test it. Black magic is not omnipotent. For example, we need to encapsulate gestures in the navigation controller to uniformly manage the left return button. These things are better inherited. Technology is a tool. Black cat, white cat, as long as it catches mice, it is a good cat. #p# 3 Should network access parameters use basic data types or objects? Let's look at two methods
When accessing network requests, there are two main methods for designing a method for requests with parameters.
Normally this doesn't make a big difference, but Han Ge's suggestion is that basic data types should never appear. In general, developers may think that there is no difference. Let me give you an example. When designing a page to display data: The logic on the page is to load the first page by default, and the length of each page is 10 (the students on the server are generally very friendly, and the default length of each page is 10 if it is not passed). However, if it is passed, it will overwrite the default parameters written in the background. For example, if 20 is passed, the server will spit out 20 data
Super hard to debug Therefore, in the network access, Han Ge's suggestion is that basic data types should never appear. 4 What are the benefits of using NSNumber over basic data types? 64-bit adaptation issues We usually use it as a parameter cache for network requests or display it on the page
I have seen friends assign values to pages like this We see that there seems to be nothing wrong with this But we switch the device to iPhone5S or below, which is a 32-bit device Note that there is a Warning Why? Let's take a look at the header file of NSInteger In 32-bit devices, it is Int; in 64-bit devices, it is Long We all know that Apple does not allow apps that do not support 64-bit to be released, but it seems that we have never adapted for 32-bit and 64-bit. In fact, the corresponding %d %zd %f placeholders in printf and NSLog are very strict. If you don't use them correctly, you will get unexpected results. In fact, when we get an NSNumber, we don't know whether it is int, long, unsigned int, Bool, and directly converting to a certain type is risky. But Clang actually provides us with a very useful Macro @() NSNumber is not a simple class. It is the implementation of a class cluster in Cocoa. http://www.cocoachina.com/ios/20140109/7681.html http://www.cocoachina.com/ios/20150106/10848.html http://www.cocoachina.com/ios/20141218/10688.html |
<<: Do you know what features App development can really improve customer retention rate?
>>: If the Internet and the economy really face a cold winter, I will give you a reassurance pill
As everyone knows, ASO refers to the search optim...
Being short is a lingering internal injury for mo...
Produced by: Science Popularization China Author:...
According to recent news, Changan Automobile rele...
Google's technology is one of the main drivin...
Yesterday afternoon, on the "Financial Day&q...
After Xiaomi became famous, the term "Weibo ...
Continuing from the previous article "18 det...
Many friends who are in the live broadcast field ...
iPhone 6s, as Apple's blockbuster product, wi...
The picture material comes from the Internet Mosq...
Content has always been an important system in th...
The disease of "ear water imbalance" ha...
In response to the fraud allegations made by shor...
【AnXin】Introduction to oral broadcasting and skil...