欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-21 08:30:52
ios 开发之objectivec的变量类型的字符代表 实例代码: nsdictionary *dic = @{@"1":[nsnumber numberwit...

ios 开发之objectivec的变量类型的字符代表

实例代码:

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); 
      } 
    } 

以上的输出结果证明:

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关键字的

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)); 

以上的输出结果证明:

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 

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

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

上一篇:

下一篇: