IOS 开发之ObjectiveC的变量类型的字符代表
IOS开发之ObjectiveC的变量类型的字符代表
实例代码:
NSDictionary*dic=@{@"1":[NSNumbernumberWithBool:YES], @"2":[NSNumbernumberWithChar:'1'], @"3":[NSNumbernumberWithDouble:2.0], @"4":[NSNumbernumberWithFloat:2.0f], @"5":[NSNumbernumberWithInt:1], @"6":[NSNumbernumberWithInteger:2], @"7":[NSNumbernumberWithLong:30], @"8":[NSNumbernumberWithLongLong:3], @"9":[NSNumbernumberWithShort:4], @"10":[NSNumbernumberWithUnsignedChar:'1'], @"11":[NSNumbernumberWithUnsignedInt:3], @"12":[NSNumbernumberWithUnsignedInteger:35], @"13":[NSNumbernumberWithUnsignedLong:45], @"14":[NSNumbernumberWithUnsignedLongLong:59], @"15":[NSNumbernumberWithUnsignedShort:4] }; for(NSString*keyindic){ idvalue=[dicvalueForKey:key]; if([valueisKindOfClass:[NSNumberclass]]){ constcharchar*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(longlong)); NSLog(@"9=>%s",@encode(short)); NSLog(@"10=>%s",@encode(unsignedchar)); NSLog(@"11=>%s",@encode(unsignedint)); NSLog(@"12=>%s",@encode(unsignedlong));//unsignedNSInteger NSLog(@"13=>%s",@encode(unsignedlong)); NSLog(@"14=>%s",@encode(unsignedlonglong)); NSLog(@"15=>%s",@encode(unsignedshort));
以上的输出结果证明:
BOOL=>c char=>c double=>d float=>f int=>i NSInteger=>q long=>q longlong=>q short=>s unsignedchar=>C unsignedint=>I unsignedNSInteger=>Q unsignedlong=>Q unsignedlonglong=>Q unsignedshort=>S
读者可以根据这种方式来判断某个变量到底属于哪种类型。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!