In-depth analysis of iPhone Category usage

Source: Internet
Author: User

In-depth analysisIPhone CategoryUsage is the content to be introduced in this article.IPhoneSome problems were encountered during the static library. Even if I use the-ObjC option in the options of the application link, my program is still running Crash on the iPhone, and after positioning, I found that it is still in the library.CategoryThe related methods are not chained in, but the program runs on the simulator but there is no problem,CategoryThe related methods are correctly executed.

So why didn't the application on the real machine chain the Category method? The answer is the apple official webpage mentioned last time. As mentioned in IMPORTANT at the bottom of this page, in 64-bit and iPhone OS systems, the-ObjC option cannot normally load the Category method of classes in the static library due to a Bug in the compiler! That is to say,

Assume that your class is called MyClass and another Category is called MyClass (Extent). If your application connects to your library, use the method in MyClass (Extent, the program will Crash! It should be noted that if your Implementation of MyClass (Extent) is in the same implementation body as that of MyClass, the Category method can still be used normally, because it will be loaded together during compilation.

So, assume that your library already uses Category. When you finally need to use it on a real machine, you will find it Crash! What should we do? In addition to code modification, there are still some remedial methods.

First, officially recommended, but often not very useful. Use the-all_load option, which causes all class methods to be loaded, regardless of whether your program is used or not. All, including the system, methods will be loaded. This method will not only increase the size of your target program, but also lead to some conflicts, which often cannot be solved in your linked libraries ). Or you can use-force_load. You can specify the library for loading all methods. This is a good solution, but it is only supported after Xcode3.2, if you have to support earlier versions of the SDK, it is unfortunate that you are not very useful in both methods! The tragedy is like me)

Second, the Category you use is the Category of your own class. This is a common situation. Your own class is large and is often used to split multiple files. After a period of thinking, it is found that the solution to this situation is relatively easy. Although I personally think it is not elegant, it can be easily solved.

You can include all your Category header files to the class's main header files or main implementation files, and directly import your various Category implementation files in the main implementation file of your class. In this way, when the compiler links the main method of your class, your various Category implementations are imported into your class main implementation file, so he can find your Category method, link the method to the target file. Of course, this method is not very elegant.

Third, you are using the Category of a class of the system API. For example, you have added a Category for UILabel: UILabel (MyExtent ). this is actually very troublesome. At the beginning, I tried a lot of methods and some methods on the Internet, but it seems that I still don't need to use it. I finally thought about it again, what is the difference between this and your own Category? It seems that there is no difference. Finally, I tried to find a class that will definitely be used and import the header file and implementation file of Category to the main implementation file of the class, in this way, the compiler can also find these Category methods. Although the problem is solved in the same way, the sentence is not elegant at all!

Although these problems have finally been solved, we can conclude that we should not use the file Category method in static libraries of iPhone 3.0 and above. However, 2.0 of SDK compilation experiments are normal.

Finally, Category, as one of the most distinctive Syntax Functions of Objective-C, cannot be normally used in static libraries. Apple doesn't think you are too fond of it! This serious bug is not fixed! Do you force me to write a class with around 1000 rows? Some classes may not be split into different classes for some reason!

Summary: in-depth analysisIPhone CategoryThe usage is complete. I hope this article will help you!

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.