你的位置:首页 > 操作系统

[操作系统]NSValueNSNumber


void testForNSValue(void)

{

    int i=10;

    

//    NSLog(@"encode(int)=%s",@encode(int));

//    NSLog(@"encode(float)=%s",@encode(float));

//    NSLog(@"encode(struct student)=%s",@encode(struct student));

    NSValue* value =[[NSValue alloc]initWithBytes:&i objCType:@encode(int) ];

    

    int ret;

    [value getValue:&ret];

    NSLog(@"ret=%i",ret);

 

 

    struct s stt={88,99};

    NSValue* value2 =[[ NSValue alloc]initWithBytes:&stt objCType:@encode(struct s)];

    

    struct s newStt;

    [value2 getValue:&newStt];

    NSLog(@"newStt.a=%i, newStt.b=%i",newStt.a,newStt.b);

    

    NSLog(@"objCType is %s", [value2 objCType]);

//    @encode(struct s) == [value2 objCType]

 

    int *p=&i;

    NSLog(@"p=%p",p);

    NSValue* v3 = [[ NSValue alloc]initWithBytes:&p objCType:@encode(int *)];

    int *q;

    [v3 getValue:&q];

    NSLog(@"q=%p",q);

    

 

}

 

void testNSNumber(void)

{

    //对4字节有符号整型

    NSNumber *obj1 = [ NSNumber numberWithInt:123];

    

    NSLog(@"obj1 is %i",[obj1 intValue]);

 

    //对8字节有符号整型

    NSNumber *obj2 = [ NSNumber numberWithInteger:88888888];

    NSLog(@"obj2 is %li", [obj2 integerValue]);

 

    //对4字节无符号整型

    NSNumber * obj3 = [ NSNumber numberWithUnsignedInt:345];

    NSLog(@"obj3 is %u", [obj3 unsignedIntValue]);

    

    //对8字节无符号整型

    NSNumber* obj4 = [ NSNumber numberWithUnsignedInteger:99999999];

    NSLog(@"obj4 is %lu", [obj4 unsignedIntegerValue]);

 

    //其它的基本数据类型的封装方法

//    NSNumber numberWithUnsignedChar:<#(unsigned char)#>

//    obj4 unsignedCharValue

    

//    NSNumber numberWithShort:<#(short)#>

//      obj4 shortValue

    

//     NSNumber numberWithUnsignedLongLong:<#(unsigned long long)#>

//     obj4 unsignedLongLongValue

    

    NSNumber* obj5 = [ NSNumber numberWithBool:YES];

    NSLog(@"obj5 is %i",[obj5 boolValue]);

    

    int a;

    NSNumber * obj6 = [NSNumber numberWithUnsignedLong:(unsigned long)&a];

    int * add =  (int *)[obj6 unsignedLongValue];

}