zoukankan      html  css  js  c++  java
  • Objectivec NSNumber和NSValue

    NSNumber

            //NSNumber创建后可以放入数组或字典中     装箱(boxing)基本类型->对象     取消装箱(unboxing)对象->基本类型
    NSNumber *n1 = [NSNumber numberWithInt:100];
    NSLog(@"n1: %@", n1);

    NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:10];
    [arr1 addObject:n1];
    NSLog(@"arr1: %@", arr1);

    NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithCapacity:10];
    [dic1 setObject:n1 forKey:@"price"];
    NSLog(@"dic1: %@", dic1);


    //NSNumber初始化
    NSNumber *n2 = [NSNumber numberWithChar:'a'];
    NSLog(@"n2: %@", n2);

    NSNumber *n3 = [NSNumber numberWithBool:false];
    NSLog(@"n3: %@", n3);

    NSNumber *n4 = [NSNumber numberWithBool:true];
    NSLog(@"n4: %@", n4);

    NSNumber *n5 = [NSNumber numberWithDouble:100.08];
    NSLog(@"n5: %@", n5);

    //NSNumber获取
    NSLog(@"n2: %c", [n2 charValue]);
    NSLog(@"n5: %f", [n5 doubleValue]);

    NSValue

            //NSValue 初始化 valueWithBytes:<#(const void *)#> objCType:<#(const char *)#>   valueWithRange等
    // NSNumber的子类,添加至数组或字典中, 基本类型(NSRect, NSRange, NSSize, NSPoint -> 对象)

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

    NSRect rect1 = NSMakeRect(3, -10, 30, 20);
    NSValue *val1 = [NSValue valueWithRect:rect1];
    [array addObject:val1];
    NSLog(@"array: %@", array);

    NSRange range1 = NSMakeRange(100, 2000);
    NSValue *val2 = [NSValue valueWithRange:range1];
    [array addObject:val2];
    NSLog(@"array: %@", array);

    NSSize size1 = NSMakeSize(10, 10);
    NSValue *val3 = [NSValue valueWithSize:size1];
    [array addObject:val3];
    NSLog(@"array: %@", array);

    NSPoint point1 = NSMakePoint(30, 50);
    NSValue *val4 = [NSValue valueWithPoint:point1];
    [array addObject:val4];
    NSLog(@"array: %@", array);

    NSRect rect2 = NSMakeRect(0, 0, 100, 300);
    NSValue *val5 = [NSValue valueWithBytes:&rect2 objCType:@encode(NSRect)];
    [array addObject:val5];
    NSLog(@"array: %@", array);

    NSPoint point2 = NSMakePoint(15, 90);
    NSValue *val6 = [NSValue valueWithBytes:&point2 objCType:@encode(NSPoint)];
    [array addObject:val6];
    NSLog(@"array: %@", array);


    //NSValue获取 getValue(要传递得是存储数值的地址) rectValue, pointValue, sizeValue
    NSValue *v1 = [array objectAtIndex:0];
    NSLog(@"v1: %@", v1);
    NSRect rect3;
    [v1 getValue:&rect3];
    NSLog(@"location: %f, length: %f", rect3.origin.x, rect3.origin.y);

    NSRect rect4 = [v1 rectValue];
    NSLog(@" %f, height: %f", rect4.size.width, rect4.size.height);


  • 相关阅读:
    写的好的功能参考地址
    碰撞检测原理
    懒加载原理的实现
    jQuery图片延迟加载插件jQuery.lazyload 的使用
    电子工厂生产楼职位解析
    打印条码方式
    条码打印二
    条码打印三
    CSS实现圆角矩形
    条码打印四
  • 原文地址:https://www.cnblogs.com/shuaixf/p/2379343.html
Copyright © 2011-2022 走看看