iOS developing basic data types and structure encapsulation and unpacking--demon Demon
Knowledge:
Because the base data types and structs are not inherited from NSObject, they cannot be stored directly into arrays and dictionaries.
Only object types can be stored in arrays and dictionaries, and other primitives and structs are not put into arrays and dictionaries, and of course you cannot send messages to them (that is, some nsobject methods cannot be called). Boxing (boxing) and unpacking (unboxing) are usually used at this time. However, the process of boxing in OBJC must be implemented manually, and OBJC does not support automatic boxing.
In OBJC we generally boxed the base data type into the NSNumber type (which is also a subclass of NSObject, but NSNumber cannot be boxed on the struct), calling its corresponding method for conversion:
@interface C (nsnumbercreation)
Encapsulation of basic data types
+ (NSNumber *) Numberwithchar: (char) value;
+ (NSNumber *) Numberwithunsignedchar: (unsigned char) value;
+ (NSNumber *) Numberwithshort: (short) value;
+ (NSNumber *) Numberwithunsignedshort: (unsigned short) value;
+ (NSNumber *) Numberwithint: (int) value;
+ (NSNumber *) Numberwithunsignedint: (unsigned int) value;
+ (NSNumber *) Numberwithlong: (long) value;
+ (NSNumber *) Numberwithunsignedlong: (unsigned long) value;
+ (NSNumber *) Numberwithlonglong: (Long long) value;
+ (NSNumber *) Numberwithunsignedlonglong: (unsigned long long) value;
+ (NSNumber *) Numberwithfloat: (float) value;
+ (NSNumber *) Numberwithdouble: (double) value;
+ (NSNumber *) Numberwithbool: (BOOL) value;
+ (NSNumber *) Numberwithinteger: (Nsinteger) value ns_available (10_5, 2_0);
+ (NSNumber *) Numberwithunsignedinteger: (Nsuinteger) value ns_available (10_5, 2_0);
Unlocking a basic data type
@property (readonly) char charvalue;
@property (readonly) unsigned char unsignedcharvalue;
@property (readonly) short shortvalue;
@property (readonly) unsigned short unsignedshortvalue;
@property (readonly) int intvalue;
@property (readonly) unsigned int unsignedintvalue;
@property (readonly) long longvalue;
@property (readonly) unsigned long unsignedlongvalue;
@property (readonly) long long longlongvalue;
@property (readonly) unsigned long long unsignedlonglongvalue;
@property (readonly) float Floatvalue;
@property (readonly) double doublevalue;
@property (readonly) BOOL boolvalue;
@property (readonly) Nsinteger IntegerValue ns_available (10_5, 2_0);
@property (readonly) Nsuinteger unsignedintegervalue ns_available (10_5, 2_0);
@property (readonly, copy) NSString *stringvalue;
Example:
Packaging
NSNumber *number=[nsnumber Numberwithint:2];
Nsarray *array=[nsarray Arraywithobject:number];
Solution to seal
[Number Intvalue];
The structure of the encapsulation and the need to introduce another type of nsvalue, in fact, the above NSNumber is the subclass of Nsvalue, it wraps some basic data types of common boxing, unboxing method, and Nsvalue can be any data type boxing, unpacking operations.
For the common structure Foundation has provided us with the specific packing method as follows:
@interface Nsvalue (nsvalueuigeometryextensions)
Encapsulation of the structural body
+ (Nsvalue *) Valuewithcgpoint: (cgpoint) point;
+ (Nsvalue *) Valuewithcgvector: (cgvector) vector;
+ (Nsvalue *) Valuewithcgsize: (cgsize) size;
+ (Nsvalue *) Valuewithcgrect: (cgrect) rect;
+ (Nsvalue *) Valuewithcgaffinetransform: (cgaffinetransform) transform;
+ (Nsvalue *) Valuewithuiedgeinsets: (uiedgeinsets) insets;
+ (Nsvalue *) Valuewithuioffset: (uioffset) insets Ns_available_ios (5_0);
The solution of the structural body
-(Cgpoint) Cgpointvalue;
-(Cgvector) Cgvectorvalue;
-(cgsize) Cgsizevalue;
-(CGRect) Cgrectvalue;
-(Cgaffinetransform) Cgaffinetransformvalue;
-(uiedgeinsets) Uiedgeinsetsvalue;
-(Uioffset) Uioffsetvalue Ns_available_ios (5_0);
Packaging
Cgpoint Point=cgpointmake (10, 20);
Nsvalue *value=[nsvalue valuewithcgpoint:point];//There is a direct way to package the system's own type
Nsarray *array=[nsarray arraywithobject:value];//put in the countdown group
Solution to seal
Cgpoint Point1=[value Cgpointvalue];
Encapsulation and unpacking of basic data types and structures (iOS development)