`
liuzhifu123
  • 浏览: 34773 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

iPhone 开发:Dictionary

阅读更多

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?)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics