• @encode关键字


    @encode()

    为了更好的互操作性,Objective-C 的数据类型,甚至自定义类型、函数或方法的元类型,都可以使用 ASCII 编码。@encode(aType) 可以返回该类型的 C 字符串(char *)的表示


        NSDictionary *dic = @{@"key1":[NSNumber numberWithBool:YES],
                              @"key2":[NSNumber numberWithDouble:77.777777f],
                              @"key3":[NSNumber numberWithInt:99],
                              @"key4":[NSNumber numberWithFloat:55.0f],
                              @"key5":[NSNumber numberWithUnsignedInteger:100]};
        for(NSString *key in dic){
            id value = [dic valueForKey:key];
            NSLog(@"value === %@",value);
            if([value isKindOfClass:[NSNumber class]]){
                const char * pObjCType = [((NSNumber*)value) objCType];
                if (strcmp(pObjCType, @encode(int))  == 0) {
                    NSLog(@"字典中key=%@的值是int类型,值为%d",key,[value intValue]);
                }
                if (strcmp(pObjCType, @encode(float)) == 0) {
                    NSLog(@"字典中key=%@的值是float类型,值为%f",key,[value floatValue]);
                }
                if (strcmp(pObjCType, @encode(double))  == 0) {
                    NSLog(@"字典中key=%@的值是double类型,值为%f",key,[value doubleValue]);
                }
                if (strcmp(pObjCType, @encode(_Bool)) == 0 || strcmp([value objCType], @encode(char)) == 0) {
                    NSLog(@"字典中key=%@的值是bool类型,值为%i",key,[value boolValue]);
                }
                if (strcmp(pObjCType, @encode(NSUInteger)) == 0 || strcmp(pObjCType, @encode(NSInteger)) == 0) {
                    NSLog(@"字典中key=%@的值是NSUInteger类型,值为%i",key,[value boolValue]);
                }
            }
            
        }
        
        /*
         2014-03-26 10:26:49.259 Test[88121:60b] value === 99
         2014-03-26 10:26:49.260 Test[88121:60b] 字典中key=key3的值是int类型,值为99
         2014-03-26 10:26:49.260 Test[88121:60b] value === 1
         2014-03-26 10:26:49.261 Test[88121:60b] 字典中key=key1的值是bool类型,值为1
         2014-03-26 10:26:49.261 Test[88121:60b] value === 55
         2014-03-26 10:26:49.261 Test[88121:60b] 字典中key=key4的值是float类型,值为55.000000
         2014-03-26 10:26:49.262 Test[88121:60b] value === 77.77777862548828
         2014-03-26 10:26:49.262 Test[88121:60b] 字典中key=key2的值是double类型,值为77.777779
         2014-03-26 10:26:49.262 Test[88121:60b] value === 100
         2014-03-26 10:26:49.262 Test[88121:60b] 字典中key=key5的值是NSUInteger类型,值为1
         2014-03-26 10:26:49.263 Test[88121:60b] EOCStringConstant =  EOCStringConstant
         */


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    ajax
    vue 思維導圖
    python项目_log日志的使用
    mysql数据库_serialisers
    常见的时间复杂度及其增长速度比较
    C++中的sort()函数
    用C++实现输入三个整数,中间用逗号隔开
    python——递归函数
    python——函数
    python——可变对象和不可变对象
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879445.html
Copyright © 2020-2023  润新知