------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------
Brief introduction
Introduced: Nsarray, Nsset, nsdictionary these OC collections can only hold OC objects and cannot hold basic data types (int, char, double ...). )。 If we want to store the basic data types in these OC collections, we will base data types (int, char, double ...). ) is packaged into OC objects, then it can be stored in the OC set. NSNumber can wrap a basic data type as an OC object.
Nsvalue: Used to store a C or OC data, you can wrap the base data type (int, double). ), you can also package the structure (Cgpoint, cgsize, CGRect).
NSNumber: Inherited from Nsvalue, which is typically used to store basic data types (int, double) in the C language. )。
Nsnumber/nsvalue1. Conversion of numeric objects to basic data types
1.int <-> Nsnumberint a = 10; NSNumber *numa = [NSNumber numberwithint:a];int Tempa = numa.intvalue;//2.double <-> nsnumberdouble b = 20.5f; NSNumber *numb = [NSNumber numberwithdouble:b];d ouble TEMPB = Numb.doublevalue;//3.char <-> Nsnumberchar ch = ' A ';// NSNumber *numch = @ (CH); NSNumber *numch = [NSNumber Numberwithchar:ch];char tempch = numch.charvalue; NSLog (@ "%c", tempch);//4.nsstring <-> Nsnumberchar *str = "string"; NSNumber *numstr = [NSNumber numberwithchar:str];char *tempstr = numstr.charvalue;//5. Compiler features/*@1 = = [NSNumber NUMBERWITHINT:1]; *@ "string" = = [NSNumber numberwithint: "string"]; *int a = 10; *@ (a) = = [NSNumber numberwithint:a]; */
2. Basic use of Digital objects
#import <foundation/foundation.h>int Main (int argc, const char * argv[]) { @autoreleasepool { //1. struct <-> Nsvalue Cgpoint a = {n}; Nsvalue *vaa = [Nsvalue valuewithpoint:a]; Cgpoint Tempa = vaa.pointvalue; The cgpoint is packaged into OC objects, which can be deposited into the OC set nsmutableset *set = [Nsmutableset set]; [Set ADDOBJECT:VAA]; The double type is wrapped into a NSNumber object, which can be stored in the set [set addobject:@10.5]; [Set Enumerateobjectsusingblock: ^ (id obj, BOOL *stop) { NSLog (@ "%@", obj); }]; } return 0;}
result output:
2015-05-10 22:47:41.018 09-nsnumber Basic use [896:38347] Nspoint: {1, 2}2015-05-10 22:47:41.019 09-nsnumber basic use [896:38,347] 10.5
Dark Horse programmer--foundation Framework Common Class (Nsnumber/nsvalue)