Runtime uses-category to add instance variables
Here's an ingenious way to solve this: using the runtime Library's function to resolve.
There are only 2 ways to use the runtime, and there are other ways
With this method, discerning eye can see that there is a set and get keyword that is, a setting is a value This method is used to simulate the build properties.
Code:
The normal declaration of a property in the. h file, not too pompous
@property (nonatomic,retain) Zbarviewcontroller *readervc;
And then in. The header file containing the runtime in M
#import <objc/runtime.h>
And then this step is very important. We need an associated key to use , this key is used to store the
//General used to mark the key perpetual three kinds of wording
1,Static void*strkey = &strKey;
2,Staticnsstring *strkey = @"Strkey";
3,Static Charstrkey;
Here I use the Third Kind
static char Addresskey;
Then start calling the runtime method
Setting the value
-(void) Setalloc: (Zbarviewcontroller *) Zbar
{
Objc_setassociatedobject (self, &addresskey, Zbar, objc_association_retain_nonatomic);
}
objc_setAssociatedObject
method, this method has four parameters, namely: source Object (self), associated object (Addresskey) (because you may want to add many properties), associated objects (Zbar) and an association policy (I have previously declared properties with retain so this is retain).
An association policy is an enumeration value
enum {
Objc_association_assign = 0, //The property of the associated object is a weak reference
Objc_association_retain_nonatomic = 1, //The property of the associated object is a strong reference and the associated object does not use atomicityObjc_association_copy_nonatomic = 3, //The property of the associated object is copy and the associated object does not use atomicity
Objc_assoCiation_retain = 01401, //The property of the associated object is copy and the associated object uses atomicity
Objc_association_copy = 01403 //The property of the associated object is copy and the associated object uses atomicity};
Take value
-(Zbarviewcontroller *) get
{
Return Objc_getassociatedobject (self, &addresskey);
}
Then initialize the method with the method to take the value of
[Self setalloc:[zbarviewcontroller new]; Call Set
[Self get].readerdelegate = self; Call get
This allows you to use attributes in the class.
This article is from the "zhuoking" blog, make sure to keep this source http://9951038.blog.51cto.com/9941038/1794904
Runtime uses-category to add instance variables