// // main.m // KenshinCui // // Created by hxd_mac on 15-6-7. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" void Test1() { //基本数据类型装箱 // +(NSNumber *)numberWithChar:(char)value; // // +(NSNumber *)numberWithInt:(int)value; // // +(NSNumber *)numberWithFloat:(float)value; // // +(NSNumber *)numberWithDouble:(double)value; // // +(NSNumber *)numberWithBool:(BOOL)value; // // +(NSNumber *)numberWithInteger:(NSInteger)value; // //基本数据类型拆箱 // -(char)charValue; // // -(int)intValue; // // -(float)floatValue; // // -(double)doubleValue; // // -(BOOL)boolValue; //NSNumber 不能对结构体装箱 NSNumber *number = [NSNumber numberWithChar:'C']; NSLog(@"number: %@", number); NSArray *array = [NSArray arrayWithObject:number]; NSLog(@"array: %@", array); NSLog(@"%c", number.charValue); } void Test2() { //常用的结构体Foundation已经为我们提供好了具体的装箱方法: // +(NSValue *)valueWithPoint:(NSPoint)point; // // +(NSValue *)valueWithSize:(NSSize)size; // // +(NSValue *)valueWithRect:(NSRect)rect; //对应的拆箱方法: // -(NSPoint)pointValue; // // -(NSSize)sizeValue; // // -(NSRect)rectValue; CGPoint point = CGPointMake(2.5, 4); NSValue *value = [NSValue valueWithPoint:point]; NSLog(@"value: %@", value); NSArray *array = [NSArray arrayWithObject:value]; NSLog(@"array: %@", array); CGPoint point2 = [value pointValue]; NSLog(@"point.x = %.1f, point.y = %.1f", point2.x, point2.y ); } typedef struct myDate{ int year; int month; int day; }myDate; void Test3() { // 自定义的结构体类型,使用以下方法进行装箱: // // +(NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type; // // 调用下面的方法进行拆箱: // // -(void)getValue:(void *)value; myDate date = {2015, 6, 8}; char *type = @encode(myDate); NSValue *value = [NSValue valueWithBytes:&date objCType:type]; NSLog(@"value: %@", value); NSArray *array = [NSArray arrayWithObject:value]; NSLog(@"array: %@", array); myDate date2; [value getValue:&date2]; NSLog(@"%d, %d, %d", date2.year, date2.month, date2.day); } void Test4() { //NSNull null单例 只有 null一个方法 NSNull *nul1 = [NSNull null]; NSNull *nul2 = [NSNull null]; NSLog(@"%@", (nul1 == nul2)?@"yes":@"no"); NSArray *array = [NSArray arrayWithObjects:@"a",nul1,@"b",nul2,nil]; NSLog(@"array: %@", array); } typedef enum { sprint = 0, summer, autumn, winter, }seaSon; void Test5() { // @符号用来装箱 NSNumber *number = @100; NSArray *array = [NSArray arrayWithObjects:@10, @"string", number, @7.2, @'c', @YES, nil]; NSLog(@"array: %@", array); NSNumber *num2 = @(1+2*3); NSLog(@"num2: %@", num2); NSNumber *num3 = @(winter); NSLog(@"num3: %@", num3); NSArray *arr2 = @[@YES, @"string", @'c', @7, @5.5]; NSLog(@"arr2[3]: %@", arr2[3]); NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr2]; arr3[0] = @"mac"; NSLog(@"arr3[0] = %@", arr3[0]); NSDictionary *dictionary = @{@1: @'a', @2: @'b', @3: @'c'}; NSLog(@"dictionary = %@", dictionary); NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithDictionary:dictionary]; dic2[@"1"] = @"yes"; NSLog(@"dic2[@\"1\"] = %@", dic2[@"1"]); } int main(int argc, const char * argv[]) { @autoreleasepool { Test1(); Test2(); Test3(); Test4(); Test5(); } return 0; }
输出结果:
2015-06-08 21:50:33.675 KenshinCui[791:303] number: 67
2015-06-08 21:50:33.684 KenshinCui[791:303] array: (
67
)
2015-06-08 21:50:33.686 KenshinCui[791:303] C
2015-06-08 21:50:33.688 KenshinCui[791:303] value: NSPoint: {2.5, 4}
2015-06-08 21:50:33.690 KenshinCui[791:303] array: (
"NSPoint: {2.5, 4}"
)
2015-06-08 21:50:33.691 KenshinCui[791:303] point.x = 2.5, point.y = 4.0
2015-06-08 21:50:33.693 KenshinCui[791:303] value: <df070000 06000000 08000000>
2015-06-08 21:50:33.695 KenshinCui[791:303] array: (
"<df070000 06000000 08000000>"
)
2015-06-08 21:50:33.697 KenshinCui[791:303] 2015, 6, 8
2015-06-08 21:50:33.699 KenshinCui[791:303] yes
2015-06-08 21:50:33.700 KenshinCui[791:303] array: (
a,
"<null>",
b,
"<null>"
)
2015-06-08 21:50:33.702 KenshinCui[791:303] array: (
10,
string,
100,
"7.2",
99,
1
)
2015-06-08 21:50:33.703 KenshinCui[791:303] num2: 7
2015-06-08 21:50:33.705 KenshinCui[791:303] num3: 3
2015-06-08 21:50:33.706 KenshinCui[791:303] arr2[3]: 7
2015-06-08 21:50:33.713 KenshinCui[791:303] arr3[0] = mac
2015-06-08 21:50:33.715 KenshinCui[791:303] dictionary = {
3 = 99;
1 = 97;
2 = 98;
}
2015-06-08 21:50:33.719 KenshinCui[791:303] dic2[@"1"] = yes