How to Use Objective-C Dictionaries

本文介绍了如何在Objective-C中使用字典,包括实例化、添加和检索对象等基本操作,并提供了一个简单的示例来帮助理解。

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

How to Use Dictionaries

Like an array, a dictionary is a list of objects. However, dictionaries make it
easier to organize data by providing “keys” that make it a snap to find the
things you put in there. These are sometimes referred to as a hash or a
hash-table.

Instantiate a dictionary

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 
Add objects to a dictionary indexed by keys

[dictionary setObject:@"A Book about the Letter A" forKey:@"A"];
[dictionary setObject:@"A Book about the Letter B" forKey:@"B"];
[dictionary setObject:@"A Book about the Letter C" forKey:@"C"];

Retrieve an object from a dictionary with a key

NSLog([dictionary objectForKey:@"B"]);
Release a dictionary

[dictionary release]; 

Cool – hope you enjoyed this snippet of da codes! Once you get use to the square brackets and verbose parameter prefixes Objective-C starts to feel like the programming languages that you are used to.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值