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

这篇博客介绍了iOS中NSDictionary的初始化、遍历、添加和删除元素的方法。包括使用`dictionaryWithContentsOfFile:`和`dictionaryWithContentsOfURL:`加载字典内容,通过`allKeys`和`allValues`获取键值,以及`setObject:forKey:`添加,`removeObjectForKey:`和`removeAllObjects`删除元素的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


8th,March,2016 Tuesday

简述

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

"In general, a key can be any object (provided that it conforms to theNSCopying protocol—see below), but note that when using key-value coding the key must be a string (seeKey-Value Coding Fundamentals). Neither a key nor a value can benil; if you need to represent a null value in a dictionary, you should useNSNull"--from apple

大体上,键可以是遵循NSCoping协议的任意对象,但大多数情况是string。 键与值都不可以是nil,如果想显示一个空值,应使用NSNull。



1. 初始化

1)

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

2)

<span style="color: rgb(63, 63, 63);">NSDictionary *dictionary = [[NSDictionary alloc] </span><span style="color:#993399;">initWithObjectsAndKeys</span><span style="color:#3f3f3f;">:
                 @"name",@"jolieyang",@"age",@"23",@"gender",@"woman", nil];</span>
3) //遍历构造器方法

<span style="color: rgb(63, 63, 63);">  NSDictionary *dictionary1 = [NSDictionary </span><span style="color:#993399;">dictionaryWithObjectsAndKeys</span><span style="color:#3f3f3f;">:
                           @"jolieyang",@"name",@"23",@"age",@"woman",@"gender",nil];</span>

4) 

+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;

// path A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary.

eg:

<span style="color:#3f3f3f;">  NSURL *url = [NSURL URLWithString:@"file:///Users/jolie/Downloads/CodeResources.plist"];// 文件完整路径</span><p class="p1"><span class="s1" style="color: rgb(63, 63, 63);">NSDictionary</span><span class="s2" style="color: rgb(63, 63, 63);"> *dict = [</span><span class="s1" style="color: rgb(63, 63, 63);">NSDictionary</span><span class="s2" style="color: rgb(63, 63, 63);"> </span><span class="s3"><span style="color:#993399;">dictionaryWithContentsOfURL</span></span><span class="s2"><span style="color:#993399;">:url</span><span style="color:#3f3f3f;">];</span></span></p>

5)

+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)aURL;

// aURL An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary.

<span style="color: rgb(63, 63, 63);">  NSDictionary *dictionary1 = [NSDictionary </span><span style="color:#993399;">dictionaryWithContentsOfFile</span><span style="color:#3f3f3f;">:@"/Users/jolie/Downloads/CodeResources"];</span>


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】字典.集合.








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值