iPhone 开发:Dictionary

本文介绍了NSDictionary的基本特性,如键值唯一性及多个对象通过数组存储的方式,并深入探讨了Cocoa Touch中UUID的生成机制及其与Core Foundation API的关系。

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

Notice: This article is just a note.

 

Here are some more important facts about NSDictionary:
• Whenever you add an object to a dictionary, the dictionary retains it. Whenever you remove an object from a dictionary, the dictionary releases it.
• There can only be one object for each key. Therefore, if you add an object to a dictionary and an object is already stored with that key, the new object is added to the dictionary and the previous one is removed.
• If you want to associate multiple objects with one key, you can add them to the dictionary as an array.
• An NSDictionary is useful when you want to name the entries within a collection. In other development environments, this is called a hash map or hash table

 

While there are many ways to hack together a unique string, Cocoa Touch has a mechanism for creating universally unique identifiers (UUIDs), also known as globally unique identifiers (GUIDs). Objects of type CFUUIDRef can represent a UUID and are generated using the time, a counter, and a hardware identifier, usually the MAC address of the ethernet card.

 

However, CFUUIDRef is not an Objective-C object; it is a C structure and part of the Core Foundation API. Core Foundation is a C API that is already included in the template projects and contains the building blocks for applications including strings, arrays, and dictionaries. Core Foundation “classes” are prefixed with CF and suffixed with Ref. Other examples include CFArrayRef and CFStringRef. Many objects in Core Foundation have an Objective-C counterpart, and NSString is the Objective-C version of CFStringRef. However, CFUUIDRef does not have an Objective-C counterpart and knows nothing at all about Objective-C. Thus, when it produces a UUID as a string, that string cannot be an NSString – it must be a CFStringRef.

 

Many Core Foundation objects can simply be typecast as their Objective-C counterpart. Here’s an example:
// Create an instance of a CFStringRef

CFStringRef someString = CFSTR("String");

// Turn it in to an NSString

NSString *coolerString = (NSString *)someString;
We call this toll-free bridging. (And it works because the structures in memory are equivalent. How smart is that?)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值