Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When determining whether an object exists in a set of objects, consider using an <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html" rel="nofollow"><code>NSSet</code></a>/<a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableSet_Class/Reference/NSMutableSet.html#//apple_ref/occ/cl/NSMutableSet" rel="nofollow"><code>NSMutableSet</code></a> object (or <a href="http://developer.apple.com/library/mac/documentation/Foundation/Reference/NSOrderedSet_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009980-CH2-DontLinkElementID_1" rel="nofollow"><code>NSOrderedSet</code></a>/<a href="http://developer.apple.com/library/mac/documentation/Foundation/Reference/NSMutableOrderedSet_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableOrderedSet" rel="nofollow"><code>NSMutableOrderedSet</code></a> if you are developing for Mac OS X 10.7 or iOS 5.0 and want to retain the order of elements in the set). An <code>NSSet</code> container is designed for efficient lookups. When an object has a decent <code>hash</code> (which most Foundation objects do), the lookup is effectively O(1), which is faster than a binary search. </p> <pre><code>NSSet *dateSet = [NSSet setWithArray:dateArray]; if ([dateSet containsObject:date1]) { // do something } </code></pre> <p>Note that it is important to construct the set once rather than converting it from an array each time, or else you'll lose any performance benefit.</p> <p>For more information, see <a href="http://developer.apple.com/library/mac/documentation/cocoa/conceptual/Collections/Articles/Sets.html" rel="nofollow">here</a>.</p> <hr> <p>Since you are wanting to check for specified dates regardless of time, you need to truncate the date values <em>before</em> adding them to the set. For example (pick better names, this is only an example):</p> <pre><code>// potentially add as a category method to NSDate - (NSDate *) dateByTruncatingTime { NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:aDate]; return [[NSCalendar currentCalendar] dateFromComponents:components]; } // ------------- somewhere else ------------- - (void) actionHappened { [myMutableSet addObject:[[NSDate date] dateByTruncatingTime]]; } - (BOOL) didActionHappenOnDate:(NSDate *) aDate { return [myMutableSet containsObject:[aDate dateByTruncatingTime]]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload