1. nsnumber:
Nsarray and nsdictionary do not allow basic data types
Nsnumber can encapsulate basic data types into objects, so that the basic data types can be indirectly put into nsarray and nsdictionary.
2. nsvalue:
Nsnumber is a subclass of nsvalue, but nsnumber only wraps the numeric type. nsvalue can wrap any value, or use nsvalue to wrap the struct and add it to a set such as nsarray and nsdictionary.
How to Create nsvalue
-(ID) intwithbytes :( const void *) value objctype :( const char *) Type
+ (Nsvalue *) valuewithbytes :( const void *) objctype :( const char *) Type
+ (Nsvalue *) value :( const void *) value vithobjctype :( const char *) Type
The value parameter is the data address to be packaged (for example, an nspoint address, you can use & to obtain the address). The type parameter is a string used to describe the data type and is generated using the @ encode command.
// Obtain the encapsulated data and save it to The value address.
-(Void) getvalue :( void *) Value
// Return the string that describes the encapsulated Data Type
-(Const char *) objtype
-(Bool) isequaltovalue :( nsvalue *) Value
3. nsnull:
The nil value is not allowed in the collection, because nil has special significance in the collection, but sometimes it is necessary to store a value indicating "nothing", so you can use nsnull, it is a subclass of nsobject.
Nsnull * n = [nsnull null]; // a singleton object. Only one singleton object can be returned.
Nsnull * n1 = [nsnull null]; //
Nsnull * n2 = [nsnull null]; //
These three pointer variables point to the same variable.
4. nsdate:
Format:
Nsdateformatter * formatter = [[nsdateformatter alloc] init];
Formatter. dateformat = @ "yyyy-mm-dd hh: mm: SS ";
Date-> string
Nsdate * Date = [nsdate date];
Nsstring * STR = [nsstring stringfromdate: date];
String-> date
Nsdate * Date = [formatter datefromstring: @ "09:30:40"];
The test demo is as follows:
# Import <Foundation/Foundation. h>
Void value (){
Cgpoint point = cgpointmake (10, 11 );
// The struct is encapsulated into an object.
Nsvalue * value = [nsvalue valuewithpoint: Point];
// Add Value
Nsmutablearray * array = [nsmutablearray array];
[Array addobject: value];
// Retrieve the value
Nsvalue * value1 = [array lastobject];
Cgpoint point1 = [value1 pointvalue];
Bool reslut = cgpointequaltopoint (point1, point );
Nslog (@ "result = % I", reslut );
}
Typedef struct {
Int year;
Int month;
Int Day;
} Date;
void value2 () {
date = {maid, 30};
// void *: pointer of any type
// transmits the structure address
// generates the corresponding type description String Based on the structure type
char * type = @ encode (date );
nsvalue * value = [nsvalue value: & date withobjctype: type];
// defines a struct variable
date date1;
// retrieve the encapsulated struct
[value getvalue: & date1];
nslog (@ "year = % I, month = % I, Day = % I ", date1.year, date. month, date. day);
}
void datecreate () {
nsdate * Date = [nsdate date];
nslog (@ "date = % @", date );
// current time + 20 seconds
date = [nsdate datewithtimeintervalsincenow: 20];
nslog (@ "date + 20 = % @", date );
// a relatively distant future time is returned immediately.
date = [nsdate distantfuture];
nslog (@ "distantfuture = % @", date );
}< br> void dateuse () {
nsdate * Date = [nsdate date];
// The number of milliseconds that have elapsed since 190
nstimeinterval interval = [date timeintervalsince1970];
nslog (@ "interval = % F ", interval);
nsdate * date2 = [nsdate date];
[date earlierdate: date2];
[date laterdate: date2];
}
void dateformat () {
nsdate * Date = [nsdate date];
nsdateformatter * formater = [[nsdateformatter alloc] init];
formater. dateformat = @ "yyyy-mm-dd hh: mm: SS";
nsstring * STR = [formater stringfromdate: date];
nslog (@ "% @", STR);
[formater release];
}
int main (INT argc, const char * argv [])
{< br> @ autoreleasepool {
value ();
nslog (@ "---- value2 -----");
value2 ();
nslog (@ "---- datecreate -----");
datecreate ();
nslog (@ "---- dateuse ------");
dateuse ();
nslog (@ "---- dateformat ----");
dateformat ();
}< br> return 0;
}