Object-c Programming tips-add attributes to class objects

Source: Internet
Author: User

Object-c Programming tips-add attributes to class objects

This problem comes from a content encapsulated by the network. due to confidentiality issues, I will only describe some of my thoughts in my spare time.


Basic Idea:

Network requests should all use a BaseNetWork of the base class, and the derived class will inherit from BaseNetWork and implement some special methods. Generally, the standard process is to pass in a block. After the network request is completed, the block callback is returned. The key here is not how to implement a network request and callback block, but how to cancel the request. Generally, the base class implements a cancelNetWork method, which performs cancel operations based on its own url.


Example:

We useDerivedNetWork * net = [DerivedNetWork alloc] init];Set its own url in the init method, and then call the base class sendRequest to send the request, waiting for the callback returned by the network. To cancel the request, do we use[Net CancelNetWork];This is definitely not good. After all, the current net pointer cannot be obtained in other methods, and it is unnecessary to save it as a member. As a result, you will think that you should use the class name for the cancel operation. CancelNetWork is defined as a class method rather than an object method. This introduces a new question: How can I obtain the object url through the class method? After meditation, one day I found the objc_setAssociatedObject method, and thought of the idea that the class object itself is also an object, and thought of a solution.


Solution:

Implementation Code of the basic NetWork class:

#define LSPerson_URL_Key @"LSPerson_URL_Key"@implementation BaseNetWork-(void)setUrl:(NSString *)url{    objc_setAssociatedObject([self class], LSPerson_URL_Key, url, OBJC_ASSOCIATION_COPY_NONATOMIC);}+(void)cancelRequest{    NSString* url =  objc_getAssociatedObject([self class], LSPerson_URL_Key);    NSLog(@"cancelRequestUrl:%@", url);}@end
The above base class has a setUrl method for the object of the derived class to set its own url. It binds the url to the class Object of self class. Then there is a cancelRequest class method that reads the url from the current class object to perform the true cancel operation. In this way, the derived class can use this method to freely cancel the request.

Code:

LPStudyRequest * lpReq = [[LPStudyRequest alloc] init];

LpReq. url = @ "http://www.baidu.com ";

// You can see the log output. Different Derived classes can get different URLs. Then use this url to pass to AF for cancellation.

[LPStudyRequest cancelRequest];


Summary:

The above is a basic idea of adding variables to a class. It is well applied in this network cancellation request. I also used it to prevent its own release in advance, like the static variables in C ++.

Note that the static variables of C ++ are different from those of the single-instance static variables used in object-c. One is a static variable of the class, and the other is a static variable of the Global Program. If the base class defines static variables and the derived class inherits, the C ++ derived class will inherit well. All the derived class objects of OC have the same memory, therefore, you can simulate this situation by binding to a class object.


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.