I. Nsvalue and NSNumber relations
Nsvalue is the parent of NSNumber, both of which convert data types to objects for ease of operation. The difference is: NSNumber is the basic data type of the sub-assembly, but the structure and pointers can not be transformed, this point nsvalue to a wide range, the structure and the pointer to the object, Nsvalue to do.
Two. Structure and object
1. First build a structure:
struct sct{ int 中文版; float Math;} Score = {80,92.5f};
2. Structure to Object
Intention? Wrap the structure type data into the Nsvalue
First constant: The address of the struct is passed in so that we know where we want to store the data //second constant: Convert the data type to be stored into a C-language string nsvalue *value = [[Nsvalue alloc] Initwithbytes:&score objctype: @encode (struct SCT)]; NSLog (@ "%s", @encode (struct SCT)); This is a string that converts the data type of a struct into a C language
Output Result:
2016-06-29 20:17:55.527 nsvalue[555:85711] {sct=if} //struct data type to C language string, I is int type, F is float type program ended with exit code:0
2. Actions to be done by objects: such as storing objects in a dictionary
Nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:value,@ "key", nil]; Nsvalue *value1 = [dic objectforkey:@ "key"]; NSLog (@ "%@", value1);
Value1 agreed to the result:
2016-06-29 20:26:05.652 nsvalue[579:112040] <50000000 0000b942> //Object address program ended with exit code:0
3.NSValue reduced to a structural body
3.1 declaring an empty struct-body variable
struct SCT score2;
3.2 The structure of the value1 package into an empty structure variable in The 3.1 step
&: Indicates where the data taken out of value1 is stored [value1 getvalue:&score2];
NSLog(@ "中文版 =%d,math =%f", Score2. 中文版, Score2. Math);
Output Result:
2016-06-29 20:38:57.380 nsvalue[603:155723] 中文版 = 80,math = 92.500000Program ended with exit code:0
Three. Pointers and objects
1. First create a pointer
Char *p = (char *) 0x1f; Casting hexadecimal numbers
2. Pointer to Object
Nsvalue *value3 = [[Nsvalue alloc]initwithbytes:&p Objctype: @encode (char *)]; NSLog (@ "%@", value3);
Output Result:
2016-06-29 20:51:16.940 nsvalue[624:197140] <1f000000 00000000>
3.NSValue revert to pointer
OC Series Advanced-nsvalue