zoukankan      html  css  js  c++  java
  • Represent nil with NSNull

    Represent nil with NSNull

      It’s not possible to add nil to the collection classes described in this section because nil in Objective-C means “no object.” If you need to represent “no object” in a collection, you can use the NSNull class:

    1 NSArray *array = @[ @"string", @42, [NSNull null] ];

      NSNull is a singleton class, which means that the null method will always return the same instance. This means that you can check whether an object in an array is equal to the shared NSNull instance:

    1 for (id object in array) {
    2     if (object == [NSNull null]) {
    3         NSLog(@"Found a null object");
    4     }
    5 }

    Working with Blocks

      Blocks can also take arguments and return values just like methods and functions.As an example, consider a variable to refer to a block that returns the result of multiplying two values:

    1 double (^multiplyTwoValues)(double, double);

      The corresponding block literal might look like this:

    1    ^ (double firstValue, double secondValue) {
    2         return firstValue * secondValue;
    3     }

      The firstValue and secondValue are used to refer to the values supplied when the block is invoked, just like any function definition. In this example, the return type is inferred from the return statement inside the block.

      If you prefer, you can make the return type explicit by specifying it between the caret and the argument list:

    1     ^ double (double firstValue, double secondValue) {
    2         return firstValue * secondValue;
    3     }

      Once you’ve declared and defined the block, you can invoke it just like you would a function:

    1     double (^multiplyTwoValues)(double, double) =
    2                               ^(double firstValue, double secondValue) {
    3                                   return firstValue * secondValue;
    4                               };
    5  
    6     double result = multiplyTwoValues(2,4);
    7  
    8     NSLog(@"The result is %f", result);

    Use __block Variables to Share Storage

      If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration. This means that the variable lives in storage that is shared between the lexical scope of the original variable and any blocks declared within that scope.

    1     __block int anInteger = 42;
    2  
    3     void (^testBlock)(void) = ^{
    4         NSLog(@"Integer is: %i", anInteger);
    5     };
    6  
    7     anInteger = 84;
    8  
    9     testBlock();

      Because anInteger is declared as a __block variable, its storage is shared with the block declaration. This means that the log output would now show:

    1 Integer is: 84

      It also means that the block can modify the original value, like this:

    1     __block int anInteger = 42;
    2  
    3     void (^testBlock)(void) = ^{
    4         NSLog(@"Integer is: %i", anInteger);
    5         anInteger = 100;
    6     };
    7  
    8     testBlock();
    9     NSLog(@"Value of original variable is now: %i", anInteger);

      This time, the output would show:

    1 Integer is: 42
    2 Value of original variable is now: 100
  • 相关阅读:
    【OpenCV学习】安防监控可疑走动报警
    【OpenCV学习】OpenMP并行化实例
    【OpenCV学习】cvConvert的使用
    【OpenCV学习】Fuzzy Logic模糊逻辑边缘提取
    C# 委托系列(一)将方法作为方法的参数
    关于dev的Gridview控件的行数据的颜色控制,根据不同的值设置不同颜色
    将gridcontrol导出到excel
    DataGridView中将某行设置为当前可见区域第一行
    如何获得窗体上控件相对于屏幕的位置?
    dev 控件 lookupedit 设置选项值
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3551209.html
Copyright © 2011-2022 走看看