Nsnumber of objective-C Foundation, nsvalue, nsdate
1. Use nsnumber as follows:
In objective-C, there is an int data type, so why should we use the numeric object nsnumber. This is because many classes (such as nsarray) require the use of objects, whereas int is not an object.
Nsnumber is a numeric object. We can use the nsnumber object to create and initialize different types of numeric objects.
In addition, you can also use the instance method to set the specified value for the nsnumber object previously allocated, all of which start with initwith, such as initwithlong.
For example:
Create and initialize class methods |
Instance Initialization Method |
Value instance method |
Numberwithchar: |
Initwithchar: |
Charvalue |
Numberwithshort: |
Initwithshort: |
Shortvalue |
... |
... |
... |
1 void test() 2 { 3 NSNumber *num = [NSNumber numberWithInt:10]; 4 5 NSDictionary *dict = @{ 6 @"name" : @"jack", 7 8 9 @"age" : num10 11 };12 13 NSNumber *num2 = dict[@"age"];14 15 16 int a = [num2 intValue];17 18 NSLog(@"%d" , a);19 }
1 # import <Foundation/Foundation. h> 2 3 int main () 4 {5 // @ 20 pack 20 into an nsnumber pair like 6 7 8 nsarray * array = @ [9 10 @ {@ "name": @ "Jack ", @ "Age": @ 20}, 11 12 @ {@ "name": @ "Rose", @ "Age": @ 25 }, 13 14 @ {@ "name": @ "Jim", @ "Age": @ 27} 15]; 16 17 18 // package various basic data types into nsnumber objects 19 @ 10.5; 20 @ Yes; 21 @ 'a'; // nsnumber object 22 23 @ ""; // nsstring object 24 25 26 27 // wrap the age variable into nsnumber object 28 int age = 100; 29 @ (AGE); 30 // [nsnumber numberwithint: Age]; 31 32 33 nsnumber * n = [nsnumber numberwithdouble: 10.5]; 34 35 36 int d = [n doublevalue]; 37 38 39 40 int A = 20; 41 42 // @ "20" 43 nsstring * STR = [nsstring stringwithformat: @ "% d", a]; 44 nslog (@ "% d ", [STR intvalue]); 45 46 Return 0; 47}
2. nsvalue usage:
Nsnumber can wrap the basic data type as an object because it inherits nsvalue
1 # import <Foundation/Foundation. h> 2 3 4 int main () 5 {6 7 // struct ---> OC object 8 9 cgpoint P = cgpointmake (10, 10 ); 10 // convert struct to value object 11 nsvalue * value = [nsvalue valuewithpoint: p]; 12 13 // convert value to corresponding struct 14 // [value pointvalue]; 15 16 nsarray * array = @ [value]; 17 18 19 20 21 return 0; 22}
3. The specific nsdate usage code is as follows:
1 void use () 2 {3 // create a time object 4 nsdate * Date = [nsdate date]; 5 // print out the time zone 0 (Beijing-East 8) 6 nslog (@ "% @", date ); 7 8 nsdate * date2 = [nsdate datewithtimeinterval: 5 sincedate: date]; 9 10 11 // number of seconds elapsed from 1970 12 nstimeinterval seconds = [date2 timeintervalsince1970]; 13 14 // [date2 timeintervalsincenow]; 15}
Date Format:
1 void date2string () 2 {3 nsdate * Date = [nsdate date]; 4 5 6 // Date formatter class 7 nsdateformatter * formatter = [[nsdateformatter alloc] init]; 8 9 // y, M, D, 10 // M minutes, s seconds, H (24) hour, H (12) hour, 11 formatter. dateformat = @ "yyyy-mm-dd hh: mm: SS"; // hh/HH 24-hour/12-hour format 12 13 nsstring * STR = [formatter stringfromdate: DATE]; 14 15 nslog (@ "% @", STR); 16}
1 void string2date() 2 { 3 // 09/10/2011 4 NSString *time = @"2011/09/10 18:56"; 5 6 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 7 formatter.dateFormat = @"yyyy/MM/dd HH:mm"; 8 9 NSDate *date = [formatter dateFromString:time];10 NSLog(@"%@", date);11 return 0;12 }
Dark Horse programmer _ objective-C: nsnumber of foundation, nsvalue, nsdate