Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate as keys for NSDictionary

Is it possible to add NSDate as keys and some arrays as it values in a NSDictionary?

I dont have any requirement of writing the dictionary to disk - archiving or unarchiving, just as this guy needs it: NSDictionary with NSDates as keys

But, why I want NSDate to be added as keys specifically is so that I can fetch all keys from the dictionary and do some computations like, fetching the dates from within a range.

Whether two objects with same date value share same memory when created?

Is it possible to have NSDate as key? Also, what other problems I might face with this assumption?

Thanks,

Raj

Edit: After posting my question, I just wrote a sample code:

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateComps setDay:1];
[dateComps setMonth:1];
[dateComps setYear:2012];
NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents:dateComps];
NSDate *date2 = [[NSCalendar currentCalendar] dateFromComponents:dateComps];
NSLog(@"Date 1 = %x, Date 2 = %x", date1, date2);

Output:

Date 1 = 7945610, Date 2 = bd03610

But does the key comparison of NSDictionary interpret these 2 NSDate objects as different?

Another Edit:

Also, if I should convert NSDate to NSString using NSDateFormatter, I cannot directly check for dates within range. I will have to perform the following steps:

  1. Get all NSString array of keys from dictionary
  2. Convert them back to array of NSDate
  3. Perform predicates and fetch dates within range
  4. Again convert back those dates to an array of Strings
  5. Now use this string keys to get values from dictionary!

So, is there any better way?

like image 307
Raj Pawan Gumdal Avatar asked Jan 09 '12 09:01

Raj Pawan Gumdal


People also ask

How do you convert NSDictionary to NSMutableDictionary?

Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.

How do you set a value in NSDictionary?

You have to convert NSDictionary to NSMutableDictionary . You have to user NSMutableDictionary in place of the NSDictionary . After that you can able to change value in NSMutableDictionary . Save this answer.

What is NSDictionary in Objective c?

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. For example, an interactive form could be represented as a dictionary, with the field names as keys, corresponding to user-entered values.

How do you add value in NSMutableDictionary?

[myDictionary setObject:nextValue forKey:myWord]; You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil).


1 Answers

Yes, NSDate objects can be used as keys for NSDictionary - any object type can be used as a key provided it supports the NSCopying protocol. Keys are compared using isEqual: and not by pointer value. See the Overview section of the NSDictionary documentation for more details.

like image 151
CRD Avatar answered Sep 27 '22 01:09

CRD