OC新語法總結

2021-06-23 05:01:26 字數 1388 閱讀 5547

1.方法與順序無關

2.列舉繫結資料型別

enum ;

typedef nsuinteger language;

3. 屬性不用寫@synthesize

4.語法簡化

4.1 nsnumber

nsnumber *value;

value = @12345;

value = @123.45f;

value = @123.45;

value = @yes;

4.2 nsarray

nsarray *array;

array = @; //空陣列

array = @[ a ]; //乙個物件的陣列

array = @[ a, b, c ]; //多個物件的陣列

4.3 nsdictionary

nsdictionary *dict;

dict = @{}; //空字典

dict = @; //包含乙個鍵值對的字典

dict = @; //包含多個鍵值對的字典

注:對可變的容器,可以用:

nsmutablearray *mutableplanets = [@[

@"mercury", @"venus", @"earth",

@"mars", @"jupiter", @"saturn",

@"uranus", @"neptune"

] mutablecopy];

5.物件下標

可以通過下標方式訪問陣列和字典的資料,類似c語言結構

5.1 nsarray

nsarray *array = @[ a, b, c ];

id obj = array[i]; //通過下標方式獲取陣列物件,替換原有寫法:array objectatindex:i];

array[i] = newobj; //也可以直接為陣列物件賦值。替換原有寫法:[array replaceobjectatindex:i withobject:newobj];

5.2 nsdictionary

nsdictionary *dict = @;

id obj = dict[k2]; //獲取o2物件,替換原有寫法:[dic objectforkey:k2];

dic[k2] = newobj; //重新為鍵為k2的物件賦值,替換原有寫法:[dic setobject:newobj forkey:k2]

6.小結

@# nsnumbers

@{} dictionaries

@「」 strings

@ arrays

@() expressions

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

1.方法與順序無關 2.列舉繫結資料型別 enum typedef nsuinteger language 3.屬性不用寫 synthesize 4.語法簡化 4.1 nsnumber nsnumber value value 12345 value 123.45f value 123.45 val...

2013 12 OC新語法小結

1.方法與順序無關 2.列舉繫結資料型別 enum typedef nsuinteger language 3.屬性不用寫 synthesize 4.語法簡化 4.1 nsnumber nsnumber value value 12345 value 123.45f value 123.45 val...

Xcode4 5出現時的OC新語法

synthesize 可以不用再寫了 如果在.件裡有?1 properynsobject aproperty 那麼可以認為 編譯器會類似在.m檔案裡生成?1 synthesizeaproperty aproperty 如果上面的都不認識 就可以認為 以下 12 3 4 5 6 7 8 9 inte ...