NSArray、NSSet、NSDictionary

/*
集合
1.NSArray\NSMutableArray
* 有序
* 高速创建(不可变):@[obj1, obj2, obj3]
* 高速訪问元素:数组名[i] 2.NSSet\NSMutableSet
* 无序 3.NSDictionary\NSMutableDictionary
* 无序
* 高速创建(不可变):@{key1 : value1, key2 : value2}
* 高速訪问元素:字典名[key]
*/

字典创建

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
        NSLog(@"**************************  ******************************");

        //方式一:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"]; //方式二:
NSArray *keys = @[@"name", @"address"];
NSArray *objects = @[@"jack", @"BeiJing"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSLog(@"集合二结果为:%@",dict2); //方式三:值健对
NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
@"jack", @"name",
@"BeiJing", @"address",
@"88888888", @"qq", nil];
NSLog(@"集合三结果为:%@",dict3);

字典高速初始化

        NSLog(@"************************ 字典高速初始化 **************************");

        //健值对
NSDictionary *dict5 = @{@"name" : @"jack", @"address" : @"Beijing"};
id obj = dict5[@"name"];
NSLog(@"%@", obj); // 返回的是键值对的个数
NSLog(@"%ld", dict5.count);

字典创建实例

        NSLog(@"************************ 字典创建实例 ****************************");

        //界面语言,程序窗口标题,确定按钮文字,提示输入
NSArray* values=[NSArray arrayWithObjects:@"欢迎登录",@"登录",@"请输入username:", nil];
NSArray* keys1=[NSArray arrayWithObjects:@"window_title",
@"confirm_button",@"input_hint", nil];
NSDictionary* dict4=[NSDictionary dictionaryWithObjects:values forKeys:keys1];
NSLog(@"count:%lu",dict.count); //NSEnumerator能够使用for-each循环高速进行迭代
NSEnumerator*keyEnums=[dict4 keyEnumerator];
int i=0;
for (NSString* key in keyEnums) {
NSLog(@"key%d:%@",i++,key);
}
NSLog(@"window_title=%@",[dict4 objectForKey:@"window_title"]);
NSLog(@"confirm_button=%@",[dict4 objectForKey:@"confirm_button"]);
NSLog(@"input_hint=%@",[dict4 objectForKey:@"input_hint"]);

可变字典

        NSLog(@"*************************** 可变字典 *****************************");

        NSMutableDictionary *dict6 = [NSMutableDictionary dictionary];
// 加入值健对
[dict6 setObject:@"jack" forKey:@"name"];
[dict6 setObject:@"BeiJing" forKey:@"address"];
[dict6 setObject:@"1206293008" forKey:@"QQ"];
[dict6 setObject:@"male" forKey:@"sex"];
[dict6 setObject:@"programming" forKey:@"hobby"];
NSLog(@"%@", dict6);
// 移除值健对
[dict6 removeObjectForKey:@"address"]; //输出姓名
NSString *str = dict6[@"name"];
NSLog(@"%@", str);

字典遍历

          NSLog(@"************************** 字典遍历 ******************************");

        // 方式一:字典是无序的
NSDictionary *dict7 = @{
@"address" : @"北京",
@"name" : @"jack",
@"qq" : @"88888888"}; NSArray *keys2 = [dict7 allKeys];
for (int i = 0; i<dict7.count; i++)
{
NSString *key = keys2[i];
NSString *object = dict7[key];
NSLog(@"%@ ~~~~~~~ %@", key, object);
} //方式二:block遍历
[dict7 enumerateKeysAndObjectsUsingBlock:
^(id key, id obj, BOOL *stop) {
NSLog(@"%@ ---- %@", key, obj); }];

字典需求排序

        NSLog(@"************************* 字典需求排序 ****************************");

        NSMutableDictionary* givenName=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:4],@"王", nil];
[givenName setObject:[NSNumber numberWithInt:8] forKey:@"周"];
[givenName setObject:[NSNumber numberWithInt:6] forKey:@"李"];
[givenName setObject:[NSNumber numberWithInt:9] forKey:@"赵"]; //keySortedByValueUsingSelector
//由于value是NSNumber类型。全部能够使用compare:进行比較
NSArray* keysOfName=[givenName keysSortedByValueUsingSelector:@selector(compare:)];
for (NSString* key in keysOfName) {
NSLog(@"key:%@",key);
}
//高速迭代(枚举)
for (NSString * key in givenName) {
NSNumber* value=[givenName valueForKey:key];
NSLog(@"[%@]=%@",key,value);
} }
return 0;

最新文章

  1. linux打包与压缩
  2. iOS-----用LLDB调试,让移动开发更简单(二)
  3. CentOS Linux VPS安装IPSec+L2TP VPN
  4. WebRequest中的工厂方法模式
  5. nodeAPI--HTTP
  6. 【LeetCode】172. Factorial Trailing Zeroes
  7. NDK jni 加载静态库
  8. .NE 学习概要
  9. 一步步学习ASP.NET MVC3 (13)——HTML辅助方法
  10. Android推送等耗电原因剖析
  11. 191. Number of 1 Bits Leetcode Python
  12. hdu_5085_Counting problem(莫队分块思想)
  13. ES6 对象的扩展(下)
  14. windows 连接Linux
  15. layui 表格内容显示更改
  16. 反序列化和序列化xml使用反射处理节点的属性
  17. 使用python-aiohttp爬取今日头条
  18. CF161D Distance in Tree(点分治)
  19. Hyperldeger Fabric踩过的坑
  20. 牛客网-《剑指offer》-变态跳台阶

热门文章

  1. (转)Unity3D协同程序(Coroutine)
  2. win7 下注册dll文件失败
  3. 细说JavaScript对象(4): for in 循环
  4. UVa11988 Broken Keyboard(练习链表使用)
  5. 设计模式之单例模式(php实现)
  6. Cocos2dx 粒子销毁问题
  7. apache 配置防盗
  8. 分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace
  9. RSA加密解密及数字签名Java实现
  10. 修改oracle内存大小