首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

IOS4 note 11 (五)

2012-07-02 
IOS4 note 11 (5)NSSet and FriendsAn NSSet is an unordered collection of distinct objects. This mean

IOS4 note 11 (5)

NSSet and Friends

An NSSet is an unordered collection of distinct objects. This means that no two objects?in a set can return YES when they are compared using isEqual:. Learning whether an?object is present in a set is much more efficient than seeking it in an array, and you can?ask whether one set ?is a subset of, or ?intersects, another set. You can walk through?(enumerate) a ?set with ?the for...in construct, ?though ?the order ?is of course undefined. You can filter a set, as you can an array. Indeed, much of what you can do with?a set is parallel to what you can do with an array, except that of course you can’t do?anything with a set that involves the notion of ordering.

An NSSet is immutable. You can derive one NSSet from another by adding or removing?elements, or you can use its subclass, NSMutableSet.

NSCountedSet, a subclass of NSMutableSet, is a mutable unordered collection of objects that are not necessarily distinct (this concept is usually referred to as a bag). It is?implemented as a set plus a count of how many times each element has been added.

Personally, ?I don’t ?find myself storing data ?in sets very much; but many ?important?Cocoa methods use them (such as those involving touch event handling), so you do?have to know about them.

?

NSDictionary and NSMutableDictionary

An NSDictionary is an unordered collection of key–value pairs. The key is usually an?NSString, though it doesn’t have to be. The value can be any object. An NSDictionary?is immutable; its mutable subclass is NSMutableDictionary.

The keys of a dictionary are distinct (using isEqual: for comparison). If you add a key-value pair to an NSMutableDictionary, then if that key is not already present, the key-value pair ?is simply added, but ?if the key ?is already present, then the corresponding?value is replaced.

You can get from an NSDictionary a list of keys, a sorted list of keys, or a list of values.?You can walk through (enumerate) a dictionary by its keys with the for...in construct,?though ?the ?order ?is ?of ?course ?undefined. ?A ?dictionary ?also ?supplies ?an ?object-Enumerator, which you can use with the for...in construct to walk through just the?values. Starting ?in ?iOS 4.0, you can also walk ?through ?the key–value pairs ?together?using a block, and you can even filter an NSDictionary by some test against its values.

?

NSNull

NSNull does nothing but supply a pointer to a singleton object, [NSNull null]. Use?this singleton object to stand for nil in situations where an actual object is required and?nil ?is not permitted. For example, you can’t use nil as ?the value of an element of a?collection ?(such ?as NSArray, NSSet, or NSDictionary), ?so ?you’d use ?[NSNull null]?instead.

Despite ?what ?I ?said ?earlier ?about ?equality, ?you ?can ?test ?an ?object ?against?[NSNull null] using the C equality operator, because this is a singleton instance and?therefore pointer comparison works.

?

Check mutable/immutable

To test whether an instance is mutable or immutable, do not ask for its class. These?immutable/mutable class pairs are all implemented as class clusters, which means that?Cocoa uses a secret class, different from the documented class you work with. This?secret class is subject to change without notice, because it’s none of your business and?you should never have looked at it in the first place. Thus, code of this form is subject?to breakage:

if ([NSStringFromClass([n class]) isEqualToString: @"NSCFArray"]) // wrong!

Learning whether an object is mutable, ask it whether it responds to a mutability?method:

if ([n respondsToSelector:@selector(addObject:)]) // right

Bear in mind also that just because a collection class is immutable doesn’t mean that?the objects it collects are immutable. They are still objects and do not lose any of their?normal behavior merely because they are pointed to by an immutable collection.

?

Property Lists

A ?property ?list ?is ?a ?string ?(XML) ?representation ?of ?data. ?The ?Foundation ?classes?NSString, NSData, NSArray, and NSDictionary are the only classes that can be converted into a property list. Moreover, an NSArray or NSDictionary can be converted?into a property list only if the only classes it collects are these classes, along with NSDate?and NSNumber. (This is why, as mentioned earlier, you must convert a UIColor into?an NSData in order to store it in user defaults; the user defaults is a property list.)

When you initialize an NSArray or NSDictionary from a property list file in this way,?the objects in the collection are all immutable. If you want them to be mutable, or if?you want to convert an instance of one of the other property list classes to a property?list, you’ll use the NSPropertyListSerialization class.

?

?

热点排行