• 《Programming with Objective-C》第七章 Values and Collections


    1.平台相关的数据类型

    These types, like NSInteger and NSUInteger, are defined differently depending on the target architecture. When building for a 32-bit environment (such as for iOS), they are 32-bit signed and unsigned integers respectively; when building for a 64-bit environment (such as for the modern OS X runtime) they are 64-bit signed and unsigned integers respectively.

      尽量使用这种平台相关的数据类型,而不是写int,double,float等基本类型,防止在不同的系统平台上,基本数据类型溢出的问题

    2.使用int还是NSInteger

    For local variables, such as a counter in a loop, it’s fine to use the basic C types if you know that the value is within the standard limits.

      写for循环的时候,用int i = 0; i < n; i++ 吧,因为你知道i会不会溢出

    3.NSNumber

      使用NSNumber来表示基本数据类型

    NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];

    NSNumber *unsignedNumber = [[NSNumber alloc] initWithUnsignedInt:42u];

    NSNumber *longNumber = [[NSNumber alloc] initWithLong:42l];

    NSNumber *boolNumber = [[NSNumber alloc] initWithBOOL:YES];

    NSNumber *simpleFloat = [NSNumber numberWithFloat:3.14f];

    NSNumber *betterDouble = [NSNumber numberWithDouble:3.1415926535];

    NSNumber *someChar = [NSNumber numberWithChar:'T'];

      使用NSNumber来表示基本数据类型之更简洁的方法

    NSNumber *magicNumber = @42;

    NSNumber *unsignedNumber = @42u;

    NSNumber *longNumber = @42l;

    NSNumber *boolNumber = @YES;

    NSNumber *simpleFloat = @3.14f;

    NSNumber *betterDouble = @3.1415926535;

    NSNumber *someChar = @'T';

      从NSNumber里面取出基本数据类型

    int scalarMagic = [magicNumber intValue];

    unsigned int scalarUnsigned = [unsignedNumber unsignedIntValue];

    long scalarLong = [longNumber longValue];

    BOOL scalarBool = [boolNumber boolValue];

    float scalarSimpleFloat = [simpleFloat floatValue];

    double scalarBetterDouble = [betterDouble doubleValue];

    char scalarChar = [someChar charValue];

      NSNumber和NSInteger之间的转化

    NSInteger anInteger = 64;

    NSUInteger anUnsignedInteger = 100;

    NSNumber *firstInteger = [[NSNumber alloc] initWithInteger:anInteger];

    NSNumber *secondInteger = [NSNumber numberWithUnsignedInteger:anUnsignedInteger];

    NSInteger integerCheck = [firstInteger integerValue];

    NSUInteger unsignedCheck = [secondInteger unsignedIntegerValue];

    4.集合类型的元素必须是OC对象

    NSArray,NSSet and NSDictionary are used to manage groups of objects, which means any item you wish to add to a collection must be an instance of an Objective-C class.

    If you need to add a scalar value, you must first create a suitable NSNumber or NSValue instance to represent it.

      集合类型的元素必须是OC对象,而不能是标量值

      所谓标量值,是指int,double等基本数据类型和NSInteger,NSUInteger,BOOL等平台相关数据类型。标量值,一般声明的时候不加星号,比如NSInteger a = 1;矢量值,一般声明的时候需要加星号,比如NSNumber *b = @1;

    5.集合快速初始化的时候有个坑

    If you do need to represent a nil value in one of the collection classes, you should use the NSNull singleton class

      Collection类,如NSArray,NSSet,NSDictionary等都是nil-terminated,所以使用类似下例的方式初始化的时候,注意一定要检查元素是否是nil啊

    //事件上报 Norcy
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: _resultModel.keyWord?_resultModel.keyWord:@"", @"searchKeyword", nil];  //这里要检查是否是nil
    
    [QLReportCtlMgr reportStrID:@"video_jce_show_search_result_page" params:dic];

      不然中间哪个元素是nil的时候,后面的元素就添加不上去了,那真的要表示nil的时候怎么办呢,除了上例的NSString使用了@""之外,还可以用NSNull这个类来代替nil

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

      由于NSNull是一个单例,[NSNull null]返回的永远是同一个对象,所以可以使用以下方法判断Collection里面哪个是空的

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

    6.快速枚举

    数组之快速枚举

    for (id eachObject in array) 
    {
        NSLog(@"Object: %@", eachObject);
    }

    字典之快速枚举(注意是key哦)

    for (NSString *eachKey in dictionary) 
    {
        id object = dictionary[eachKey];
        NSLog(@"Object: %@ for key: %@", object, eachKey);
    }

    高大上的NSEnumerator

    for (id obj in [array objectEnumerator])  //正向
    {
        NSLog(@"%@", obj);    
    }
    
    for (id obj in [array reverseObjectEnumerator]) //反向
    {
        NSLog(@"%@", obj);    
    }
    
    while (id obj = [enumerator nextObject])    //nextObject+while
    {
        NSLog(@"%@", obj);
    }

    如果,你离不开for循环的下标

    int index = 0;
    for (id obj in [array objectEnumerator])  //正向
    {
        NSLog(@"%@", obj);
        index++;
    }

      

  • 相关阅读:
    第39周星期日中秋节杂记
    php array_multisort
    php统计近一周和近30天的用户数据
    什么是CGI、FastCGI、PHPCGI、PHPFPM、SpawnFCGI?
    PHP array_multisort()函数超详细理解
    微博第三方登陆请求授权出现错误码:21322(重定向地址不匹配)的解决方法
    艾伟_转载:C# 反射技术应用 狼人:
    艾伟_转载:HttpApplication的认识与加深理解 狼人:
    艾伟_转载:C# .NET学习经验总结 狼人:
    艾伟_转载:C# 委托的同步调用和异步调用 狼人:
  • 原文地址:https://www.cnblogs.com/chenyg32/p/4880915.html
Copyright © 2020-2023  润新知