@encode关键字
2014-03-26 10:32 三戒1993 阅读(124) 评论(0) 编辑 收藏 举报@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 */
版权声明:本文为博主原创文章,未经博主允许不得转载。