#warning Do not override methods in categories

#warning Do not override methods in categories

[[151478]]

In iOS development, category is a very useful thing. (Pure text, short and concise)

We can add methods and properties to a class. For example, NSDateFormatter has a very large overhead of creating a new one every time, which is particularly evident in tableview. With category, we can write a singleton for it to avoid opening up new memory for it every time.

So, we thought, what would happen if we used category to override the method of this class? It was actually feasible and it was indeed overwritten... However, as the future flowers of our motherland, we cannot do this!

Here are the reasons:

1. Category cannot replace subclasses. It cannot call the parent class's method implementation through super like a subclass. If a category overrides a method in the current class, the original method implementation in the current class will never be executed, which is fatal in some methods. (ps: Here, the +(void)load method is a special case. It will be executed in the category after the current class is executed.)

2. At the same time, a category cannot reliably override the same method of the same class in another category. For example, if UIViewController+A and UIViewController+B both override viewDidLoad, we cannot control who overrides whom.

3. By observing the header files, we can find that many classes in the Cocoa framework implement functions through categories. You may inadvertently overwrite one of these methods, which may sometimes cause some unresolved exceptions.

4. The creation of category is just to make it easier for developers to expand a class. Its original intention is not to let you change a class.

in conclusion:

To override methods, of course we first recommend overriding the parent class method through the subclass. In some cases where it is inconvenient to override, we can also use runtime to perform method swizzling in the category to achieve it.

<<:  An Incomplete Guide to Programmer Interviews

>>:  Xiaomi’s cost-effectiveness crisis has finally emerged. What is the road to future redemption?

Recommend

Case Study: A Specific App Retention Improvement Analysis Plan

This article mainly uses a case analysis to demon...

A guide to marketing and screen-sweeping on Bilibili’s “Houlang”!

During the May 4th Youth Day not long ago, "...

Detailed analysis of IntentFilter matching rules

[[430933]] Preface In daily Android development, ...

So content marketing can be done this way?

Nowadays, content marketing is regarded as a trea...

What is the meaning of Wenchang Tower? What is the function of Wenchang Tower?

Wenchang Tower can promote academic success. Many...

Cocos Studio V2.1 Open Plan Announced

On December 19, Cocos Studio, a product of the Co...

World Immunization Week丨Why do we need to get vaccinated?

Why do we need to get vaccinated? Vaccination is ...

Bidding promotion: How to optimize core words and effectively manage keywords?

We have compiled a list of common bidding problem...

Summary of live data analysis report template

This data analysis article of over 10,000 words w...

Qualcomm announces $40 million strategic investment in touch technology

Qualcomm held a press conference in Beijing today...

A card collection activity aimed at attracting new customers!

I believe everyone is familiar with the card coll...