iOS--NSDictionary字典的初始化增删


8th,March,2016 Tuesday

简述

数组是有序的,字典集合是无序的。


1. 初始化

1)

 NSDictionary *dic = @{@"name":@"jolieyang",@"age":23,@"gender":@"woman"};

2)

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                 @"name",@"jolieyang",@"age",@"23",@"gender",@"woman", nil];
3) //遍历构造器方法

  NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"jolieyang",@"name",@"23",@"age",@"woman",@"gender",nil];


2.遍历字典

1)获取所有的keys和values

allKeys allValues

2) 遍历

for(NSString *str in [tmpDict allKeys])


3. 添加元素

[tmpDict setObject:@"jolieYang" forKey:@"name"]


4. 删除元素

1) 按key删除

[tmpDict removeObjectFromKey:@"name"];

2) 删除所有元素

[tmpDict removeAllObjects];


未完待续。。。敬请期待


参考资料:

【学习ios之路:Object-C】字典.集合.








你可能感兴趣的:(iOS--NSDictionary字典的初始化增删)