IOS学习之NSDictionary和NSMutableDictionary详解

字典的操作是根据键和值的操作,一个key有相对应的value的存在,也就是说通过key值我们可以找到相对应的value

ok ,我们先来学习下不可变的NSDictionary

创建字典

+(instancetype)dictionary //creates and return an empty dictionary


有时候我们将字典的信息放入文件中,我们需要将我们所需要的信息取出可以采用

+(NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path

//creates and returns a dictionary using the keys and values found in a file specified by a given path


当然有时候也可以根据特定的URL来获取字典信息

+(NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url

//creates and return a dictionary using the kyes and values founds in a resource specified by a given URl


在更多的初始化过程中我们一般直接将键和值都写出来

+(instancetype)dictionaryWithObjectsAndKeys:(id)firstObject

eg:

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key",nil];

//creates and returns a dictionary containing entries consturcted form the specified set of values and keys


计数项

在开发过程中,我们经常需要知道字典中的数量

@property(readonly)NSUinteger count

//the number of entries in the dictionary


字典比较

-(bool)isEqualToDictionary:(NSDictionary *)otherDictionary

//Returns a Boolean value that indicates whether the contents of receiving dictionary are eaual to the contents of another given dictionary


访问键和值

@property(readonly,copy)NSArray *allKeys

//a new array containing the dictionary's keys or an empty array if the dictionary has no entries


@property(readonly,copy)NSArray *allValues

// A new array containing the dictionary's value or an empty array if the dictionary has no entries


//在操作过程中我们往往需要通过键值来查找相对应的vlaue

-(id)objectForKey:(id)key

//returns the value associates with a given key


枚举字典


当你想通过枚举的方式获得所有键的时候可以采用

-(NSEnumerator *)keyEnumerator

//returns an emumerator object that lets you access each key in the dictionary


所有对象的枚举对象

-(NSEmumerator *)objectEnumerator

//Returns an emunerator object that let youy access each value in the dictionary


存储字典


-(BOOL)writeToFile:(NSString *)path atomacially:(BOOL)flag

//writes a property list representation of the contents of the dictionary to given path


-(bool)writeToUrl:(NSURL *)aURL atomatically:(BOOL)flag

//Writes a property list representation of the contents of the dictionary to given URL


下面来接收NSMutableDictionary


首先创建和初始化的过程和NSDictionary一样处理

接下来我们要处理的是添加和删除的操作


添加操作

-(void)setObject:(id)anObject  forKey:(id<NSCopying>)akey

//Adds a given key-vlaue pair to dictionary


删除操作


-(void)removeObjectForKey(id)akey

//Removes a given key and its associated value from the dictionary


-(void)removeAllObjects

//Empties the dictionary of its entries


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值