//
// main.m
// 数组
//
// Created by Markrry-hao on 16/1/12.
// Copyright © 2016年 mac. All rights reserved.
/*
*/
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 字典的创建:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"悟空" forKey:@"大师兄"];
NSLog(@"%@",[dict objectForKey:@"大师兄"]);
NSArray *key = [NSArray arrayWithObjects:@"大师兄",@"二师兄",@"沙师弟",nil];
NSArray *obj = @[@"悟空",@"八戒",@"沙和尚",];
// 第三种方式
dict = [NSDictionary dictionaryWithObject:obj forKey:key];
// 第四种方式
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"",@"",
@"",@""
, nil];
// 第五种方式: key|value
dict = @{@"悟空":@"大师兄",@"八戒":@"二师兄",@"沙和尚":@"沙师弟"};
NSLog(@"%@",[dict objectForKey:@"沙师弟"]);
// 可变数组
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
[dict1 setObject:@"悟空" forKey:@"大师兄"];
[dict1 setObject:@"八戒" forKey:@"二师兄"];
[dict1 setObject:@"沙和尚" forKey:@"沙师弟"];
NSLog(@"%@",dict1[@"二师兄"]);
// 遍历!
// 第一种遍历
[dict1 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key==%@,value=====%@",key,obj);
}];
// 第二种遍历
NSArray *keys=[dict1 allKeys];
for (id obj in keys) {
NSLog(@"key====%@,value===%@",obj,dict1[obj]);
}
// 删除
[dict1 removeObjectForKey:@"二师兄"];
for (id obj in keys) {
NSLog(@"key====%@,value===%@",obj,dict1[obj]);
}
// 批量删除
NSArray *arr = @[@"大师兄",@"沙师弟"];
[dict1 removeObjectForKey:arr];
keys = [dict allKeys];
for (id obj in keys) {
NSLog(@"key====%@,value===%@",obj,dict1[obj]);
}
// 修改 不能修改key的值,只能修改value的值 key值无法修改,只能添加
[dict1 setObject:@"齐天大圣" forKey:@"大师兄"];
[dict1 setObject:@"齐天大圣" forKey:@"大大师兄"];//表示新添加一个键值
}
return 0;
}
|
|