Object-c Plan tips-Add to class object properties

Source: Internet
Author: User

This problem from the content of the network package, due to the secrecy issue, I just described my amateur idea.


The basic idea:

Network requests, we should be able to use the base class Basenetwork, then inherit basenetwork from the derived class, and implement some special methods.

The general standard procedure is to pass in the block when the network request is complete. Back through block callbacks. The key here is not how to implement a network request and callback block related content, but how to cancel the request. The general base class implements a Cancelnetwork method. It does the cancel operation according to its own URL.


Example:

We use derivednetwork* net = [Derivednetwork alloc]init]; The URL of its own is set in the Init method. Then call the base class's sendrequest to send the request. The callback that waits for the network to return. If we need to cancel the request, do we use [net cancelnetwork];

After all, it is not possible to get the current net pointers in other methods, and it is not necessary to save them as members. So you will think that you should use the class name for the cancel operation. The cancelnetwork is defined as a class method. Instead of the object method.

This introduces a new question, how does the class method get the URL to the object? After pondering, one day found the Objc_setassociatedobject method. Also think of the class object itself is the idea of the object, think of a solution.


Scheme:

Implementation code for the base class 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 base class above has a SetUrl method that the object of the derived class uses to set its own URL, which binds the URL to the class object of the self class. Then another Cancelrequest class method. It reads from the current class object to the URL for a real cancel operation. Through such a way. Derived classes are able to use such methods for a free cancel request operation.

Code:

lpstudyrequest* lpreq = [[Lpstudyrequest alloc]init];lpreq.url = @ "http://jiawei.com";//Can see the output of the log. Different derived classes can get different URLs. Then use this URL to pass to AF for cancellation.

[Lpstudyrequest Cancelrequest];



Summarize:

Above is a main idea of adding variables to a class. It has a very good application in this cancellation network request. I also use it to prevent myself from being released early, imitating the static variables of C + +.

It is important to note that the static variables in C + + are not the same as the single static object-c used in the case. One is a static variable belonging to a class, and one is a static variable that belongs to the global program. Suppose the base class defines a static variable. Derived classes inherit, the derived classes of C + + are very well inherited. All of the derived class objects of OC are of the same internal deposit, which can be considered using class objects, which is bound to simulate this situation.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Object-c Plan tips-Add to class object properties

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.