服务器之家

服务器之家 > 正文

IOS 开发之ObjectiveC的变量类型的字符代表

时间:2021-03-26 15:46     来源/作者:nunchakushuang

IOS 开发之ObjectiveC的变量类型的字符代表

实例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
NSDictionary *dic = @{@"1":[NSNumber numberWithBool:YES],
                @"2":[NSNumber numberWithChar:'1'],
                @"3":[NSNumber numberWithDouble:2.0],
                @"4":[NSNumber numberWithFloat:2.0f],
                @"5":[NSNumber numberWithInt:1],
                @"6":[NSNumber numberWithInteger:2],
                @"7":[NSNumber numberWithLong:30],
                @"8":[NSNumber numberWithLongLong:3],
                @"9":[NSNumber numberWithShort:4],
                @"10":[NSNumber numberWithUnsignedChar:'1'],
                @"11":[NSNumber numberWithUnsignedInt:3],
                @"12":[NSNumber numberWithUnsignedInteger:35],
                @"13":[NSNumber numberWithUnsignedLong:45],
                @"14":[NSNumber numberWithUnsignedLongLong:59],
                @"15":[NSNumber numberWithUnsignedShort:4]
                };
    for(NSString *key in dic){
      id value = [dic valueForKey:key];
      if([value isKindOfClass:[NSNumber class]]){
        const charchar * pObjCType = [((NSNumber*)value) objCType];
        NSLog(@"%@,%s",key,pObjCType);
      }
    }

以上的输出结果证明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
UnsignedChar  =>s
Char      =>c
UnsignedShort  =>i
LongLong    =>d
UnsignedInt   =>q
Float      =>f
Int       =>i
UnsignedInteger =>q
Integer     =>q
UnsignedLong  =>q
Long      =>q
LongLong    =>q
UnsignedLongLong=>q
Bool      =>c
Short      =>s
double     =>d

再看以下代码,用@encode关键字的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSLog(@"1=>%s",@encode(BOOL));
NSLog(@"2=>%s",@encode(char));
NSLog(@"3=>%s",@encode(double));
NSLog(@"4=>%s",@encode(float));
NSLog(@"5=>%s",@encode(int));
NSLog(@"6=>%s",@encode(NSInteger));
NSLog(@"7=>%s",@encode(long));
NSLog(@"8=>%s",@encode(long long));
NSLog(@"9=>%s",@encode(short));
NSLog(@"10=>%s",@encode(unsigned char));
NSLog(@"11=>%s",@encode(unsigned int));
NSLog(@"12=>%s",@encode(unsigned long));//unsigned NSInteger
NSLog(@"13=>%s",@encode(unsigned long));
NSLog(@"14=>%s",@encode(unsigned long long));
NSLog(@"15=>%s",@encode(unsigned short));

以上的输出结果证明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BOOL=>c
 char=>c
 double=>d
 float=>f
 int=>i
 NSInteger=>q
 long=>q
 long long=>q
 short=>s
 unsigned char=>C
 unsigned int=>I
 unsigned NSInteger=>Q
 unsigned long=>Q
 unsigned long long=>Q
 unsigned short=>S

读者可以根据这种方式来判断某个变量到底属于哪种类型。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/nunchakushuang/article/details/39317309

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部