OC中文法糖,最新文法總結

來源:互聯網
上載者:User

標籤:文法   oc   點文法   

  1. <span style="font-size:24px;">    1.方法與順序無關    

  2.         

  3.         

  4.     2.枚舉綁定資料類型    

  5.     enum {    

  6.         ObjectiveC,    

  7.         Java,    

  8.         Ruby,    

  9.         Python,    

  10.         Erlang    

  11.     };    

  12.     typedef NSUInteger Language;    

  13.         

  14.         

  15.     3. 屬性不用寫@synthesize    

  16.         

  17.         

  18.     4.文法簡化    

  19.     4.1 NSNumber    

  20.     NSNumber *value;    

  21.     value = @12345;    

  22.     value = @123.45f;    

  23.     value = @123.45;    

  24.     value = @YES;    

  25.         

  26.         

  27.     4.2 NSArray    

  28.     NSArray *array;    

  29.     array = @[];               //空數組    

  30.     array = @[ a ];          //一個對象的數組    

  31.     array = @[ a, b, c ]; //多個對象的數組    

  32.         

  33.         

  34.     4.3 NSDictionary    

  35.     NSDictionary *dict;    

  36.     dict = @{};     //空字典    

  37.     dict = @{ k1 : o1 };     //包含一個索引值對的字典    

  38.     dict = @{ k1 : o1, k2 : o2, k3 : o3 }; //包含多個索引值對的字典    

  39.         

  40.         

  41.     註:對可變的容器,可以用:    

  42.     NSMutableArray *mutablePlanets = [@[    

  43.        @"Mercury", @"Venus", @"Earth",    

  44.        @"Mars", @"Jupiter", @"Saturn",    

  45.        @"Uranus", @"Neptune"    

  46.      ] mutableCopy];    

  47.         

  48.         

  49.         

  50.         

  51.     5.對象下標    

  52.     可以通過下標方式存取數組和字典的資料,類似C語言結構    

  53.         

  54.         

  55.     5.1 NSArray    

  56.     NSArray *array = @[ a, b, c ];    

  57.     id obj = array[i];     //通過下標方式擷取數組對象,替換原有寫法:array objectAtIndex:i];    

  58.     array[i] = newObj;     //也可以直接為數組對象賦值。替換原有寫法:[array replaceObjectAtIndex:i withObject:newObj];    

  59.         

  60.         

  61.     5.2 NSDictionary    

  62.     NSDictionary *dict = @{ k1 : o1, k2 : o2, k3 : o3 };    

  63.     id obj = dict[k2];     //擷取o2對象,替換原有寫法:[dic objectForKey:k2];    

  64.     dic[k2] = newObj;  //重新為鍵為k2的對象賦值,替換原有寫法:[dic setObject:newObj forKey:k2]    

  65.         

  66.         

  67.     6.小結    

  68.     @# nsnumbers    

  69.     @{} dictionaries    

  70.     @“” strings    

  71.     @[] arrays    

  72.     @() expressions  </span>  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.