标签:
知识点
1.NSDictionary
2.NSMutableDictionary
===========================
NSDictionary
1.思考:现在需要通过名字查找到指定的电话号码
1)通过NSArray是否可以实现
2)是否容易实现
2.NSDictionary
1)如何创建一个字典对象
dictionaryWithObjectsAndKeys消息
作用:根据指定的对象和keys来创建一个NSDictionary对象
2)如何保存内容
通过key-value方式保存
3)什么是键值,什么是键,什么是值
4)字典的存储方式
字典是一种无序保存的集合
3.如何通过key获取对应的value
1)objectForKey消息
作用:通过指定key来查找对应的value
4.如何遍历字典内容
1)keyEnumerator消息
作用:获取所有的key值
2)allKeys消息
作用:获取所有的key值
3)allValues消息
作用:获取所有的values值
5.NSDictionary和NSArray的对比
1)字典相对与数组的优点
字典获取元素速度比数组快,原因是字典通过哈希算法得到每个元素具体的位置
2)获取元素的方式
数组通过下标获取指定元素的内容 objectAtIndex
字典通过key来获取指定元素的内容 objectForKey
6.遍历字典
1)普通遍历
2)快速遍历
============================
NSMutableDictionary
1.NSMutableDictionary和NSDictionary的区别
2.如何创建
1)dictionary消息
作用:创建一个空的字典
2)dictionaryWithObjects:forKeys消息
作用:通过指定数组来创建一个字典对象
3.可变字典的添加
1)setObject:forKey消息
作用:添加一组新的键值映射关系
4.可变字典的删除
1)removeObjectForKey消息
作用:通过指定key删除一组映射
2)removeObjectForKeys消息
作用:通过指定的一个数组内容来删除相关的映射关系
3)removeAllObjects消息
作用:删除字典中所有的映射
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//查找-引用NSdictional
NSArray*array=@[@"key1",@"value1",@"key2",@"calue2"];
//NSString*string=@"key1";
//NSString*string1=array[[array indexOfObject:string]+1];
NSString*string=array[[array indexOfObject:@"key1"]+1];
NSLog(@"%@",string);
#pragma make字典的创建
#pragma make通过+方法创建字典
//通过+方法创建单个创建
NSDictionary*dic1=[NSDictionary dictionaryWithObject:@"value1" forKey:@"key1"];
NSLog(@"dic1:%@",dic1);
//根据key获取value
NSLog(@"key1:%@",[dic1 objectForKey:@"key1"]);
NSLog(@"key1:%@",[dic1 objectForKeyedSubscript:@"key1"]);
//通过+方法创建多个创建
NSArray*keys=@[@"key1", @"key2"];
NSArray*objs=@[@"value1", @"value2"];
NSDictionary*dic2=[NSDictionary dictionaryWithObjects:objs forKeys:keys];
NSLog(@"dic2:%@",dic2);
NSLog(@"key1:%@",[dic2 objectForKey:@"key1"]);
NSLog(@"key2:%@",[dic2 objectForKeyedSubscript:@"key2"]);
#pragma make通过实例创建字典
NSDictionary*dic3=[[NSDictionary alloc]initWithObjectsAndKeys:@"value1", @"key1",
@"value2", @"key2",
nil];
NSLog(@"dic3:%@",dic3);
NSLog(@"key1:%@",[dic3 objectForKey:@"key1"]);
NSLog(@"key2:%@",[dic3 objectForKeyedSubscript:@"key2"]);
#pragma make根据现有的字典创建字典
NSDictionary*dic4=[NSDictionary dictionaryWithDictionary:dic3];
NSLog(@"dic4:%@",dic4);
NSLog(@"key1:%@",[dic4 objectForKey:@"key1"]);
NSLog(@"key2:%@",[dic4 objectForKeyedSubscript:@"key2"]);
#pragma make字典字面量语法@{key:value,key1:value}
NSDictionary*dic5=@{@"key1":@"value1", @"key2":@"value2"};
NSLog(@"%@",dic5);
NSLog(@"key1:%@",dic5[@"key1"]);
NSLog(@"key2:%@",dic5[@"key2"]);
#pragma make获取字典数量
NSUInteger leng=dic5.count;
NSLog(@"dic5.count:%ld",leng);
#pragma make键值的集合
//所有的键集合
NSArray*keys1=[dic5 allKeys];
NSLog(@"键集合:%@",keys1);
//所有值集合
NSArray*value1=[dic5 allValues];
NSLog(@"%@",value1);
#pragma make可变字典
NSMutableDictionary*MutableDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"mvalue1", @"mkey1",
@"mvalue2", @"mkey2",nil];
#pragma make添加删除插入
#pragma make添加现有字典的字典
[MutableDic addEntriesFromDictionary:dic5];
NSLog(@"%@",MutableDic);
#pragma make添加新的键值对象setValue
[MutableDic setValue:@"mValue3" forKey:@"mKey3"];
NSLog(@"%@",MutableDic);
#pragma make以新的字典数据覆盖旧的字典数据setDictionary
[MutableDic setDictionary:dic5];
NSLog(@"%@",MutableDic);
#pragma make根据key删除value
[MutableDic removeObjectForKey:@"key1"];
NSLog(@"%@",MutableDic);
#pragma make删除所有元素
[MutableDic removeAllObjects];
NSLog(@"%@",MutableDic);
#pragma make遍历
NSMutableDictionary*MutableDic1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"mValue1",@"mKey1",@"mValue2",@"mKey2", nil];
#pragma make快速遍历
for (id key1 in MutableDic1) {
NSLog(@"key:%@ value:%@",key1 ,[MutableDic1 objectForKey:key1]);
}
#pragma make枚举变量[可变数组 keyEnumerator]
//id key2=[[MutableDic1 keyEnumerator]nextObject];
NSEnumerator*enumerator=[MutableDic1 keyEnumerator];
id key2=[enumerator nextObject];
while (key2) {
NSLog(@"%@:%@",key2,[MutableDic1 objectForKey:key2]);
key2=[enumerator nextObject];
}
}
return 0;
}
/*
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
#if 0
NSArray *array = @[@"中国电信", @"10000", @"中国移动", @"10086"];
NSString *name = @"中国电信";
NSString *phone = array[[array indexOfObject:name]+1];
NSLog(@"%@", phone);
//#endif
Person *p1 = [[Person alloc] initWithName:@"中国电信" andPhone:@"10000"];
Person *p2 = [[Person alloc] initWithName:@"中国移动" andPhone:@"10086"];
NSArray *array = @[p1, p2];
NSString *name = @"中国电信";
for (Person *p in array)
{
if ([[p getName] isEqualToString:name])
{
NSLog(@"%@", [p getPhone]);
break;
}
}
#endif
//字典 key-value (键值对)
//key值在字典中唯一存在
//字典是无序的集合 哈希表 hash Table
//实例方法创建
// NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"10000", @"中国电信", @"10086", @"中国移动", nil];
NSArray *objs = @[@"10000", @"10086"];
NSArray *keys = @[@"中国电信", @"中国移动"];
//通过类方法创建
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
NSLog(@"%@", dic);
NSString *name = @"中国电信";
//获取字典的值
NSString *phone = [dic objectForKey:name];
NSLog(@"%@", phone);
//字典的字面量语法
//@{ key:value, key:value};
NSDictionary *dic2 = @{@"中国电信":@"10000", @"中国移动":@"10086"};
//获取字典值的字面量语法
NSLog(@"%@", dic2[@"中国移动"]);
//对字典的遍历!!
//枚举器法
NSEnumerator *enumer = [dic2 keyEnumerator];
id obj = nil;
while (obj = [enumer nextObject])
{
NSLog(@"%@", dic2[obj]);
}
NSLog(@"===========猥琐的分割线============");
//快速枚举字典,得到的也是key值
for (id obj in dic2)
{
NSLog(@"%@", dic2[obj]);
}
NSLog(@"===========猥琐的分割线============");
//直接获得值的数组
NSArray *values = [dic2 allValues];
NSLog(@"values:%@", values);
//直接获得所有的key值
NSArray *keys2 = [dic2 allKeys];
NSLog(@"keys:%@", keys2);
//通过value值获取key
NSArray *keys3 = [dic2 allKeysForObject:@"10000"];
NSLog(@"keysForObj:%@", keys3);
/*
数组和字典的区别
1. 数组有序集合
字典无序集合
2. 获取元素方式:
数组通过下标
字典通过key
3. 查找效率
字典的效率高一些,
*/
/*
//可变的字典
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
// -description
//添加元素
[mDic setObject:@"10010" forKey:@"中国联通"];
NSLog(@"mDic : %@", mDic);
[mDic addEntriesFromDictionary:dic2];
NSLog(@"mDic : %@", mDic);
//会覆盖之前的内容,key有且只有一个
[mDic setObject:@"xxxxx" forKey:@"中国移动"];
NSLog(@"mDic : %@", mDic);
//删除字典
[mDic removeObjectForKey:@"中国联通"];
NSLog(@"mDic : %@", mDic);
//同时删除多个key值
[mDic removeObjectsForKeys:@[@"中国移动", @"ddddd"]];
NSLog(@"mDic : %@", mDic);
NSString *str = [mDic objectForKey:@"222"];
NSLog(@"%@", str); //nil
//删除所有内容
[mDic removeAllObjects];
NSLog(@"mDic : %@", mDic);
}
return 0;
}
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
NSString *_phone;
}
- (instancetype)initWithName:(NSString *)name andPhone:(NSString *)phone;
- (NSString *)getName;
- (NSString *)getPhone;
Person.m
#import "Person.h"
@implementation Person
- (instancetype)initWithName:(NSString *)name andPhone:(NSString *)phone
{
if (self = [super init])
{
_name = name;
_phone = phone;
}
return self;
}
- (NSString *)getName
{
return _name;
}
- (NSString *)getPhone
{
return _phone;
}
@end
@end
*/
标签:
原文地址:http://www.cnblogs.com/sunbinboke/p/4728181.html