1.方法与顺序无关
2.枚举绑定数据类型
enum {
ObjectiveC,
Java,
Ruby,
Python,
Erlang
};
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 = @{ k1 : o1 };
//包含一个键值对的字典
dict = @{ k1 : o1, k2 : o2, k3 : o3 };
//包含多个键值对的字典
注:对可变的容器,可以用:
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 = @{ k1 : o1, k2 : o2, k3 : o3 };
id obj = dict[k2];
//获取o2对象,替换原有写法:[dic objectForKey:k2];
dic[k2] = newObj;
//重新为键为k2的对象赋值,替换原有写法:[dic setObject:newObj forKey:k2]
6.小结
@
# nsnumbers
@{} dictionaries
@“” strings
@[] arrays
@() expressions
OC Simple grammar grammar sugar