溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

IOS 開發(fā)之ObjectiveC的變量類型的字符代表

發(fā)布時間:2020-10-22 20:59:41 來源:腳本之家 閱讀:165 作者:lqh 欄目:移動開發(fā)

IOS 開發(fā)之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 

讀者可以根據這種方式來判斷某個變量到底屬于哪種類型。

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI