0

I am adding dates into dictionary as the key and the corresponding value as some text ,but I am converting the date into string ,before adding to a dictionary,I have written the below code

[dateNoteDict setValue:datestring forKey:notes.text];

but before adding to dictionary I want to make some date comparision such that ,if the date present in dictionary is greater than ,the nest date then ,I should add below in the dictionary and so..on..

But I am not understanding how to do that.

So friends,please help me out..

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121

2 Answers2

0

You should do the comparison before converting the dates into NSStrings.

Assuming you are using NSDate objects, you can use the compare method. Have a look at this answer from S.O about date comparison.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • hey thanks for your reply..Hey but how about the comparison of date already present in the dictionary? – Ranjit Jan 16 '12 at 10:19
  • You could convert them back to NSDates... But why don't you store your keys as NSDate in the first place? – sergio Jan 16 '12 at 10:21
  • because the doc says that,the key and value should be of NSString type – Ranjit Jan 16 '12 at 10:25
  • that is true only if you are going to store the dictionary as a plist... otherwise you can use any class that supports NSCopying protocol. In the plist case, you could use `NSKeyedArchiver` to store to disk. – sergio Jan 16 '12 at 10:32
  • Ya you are right,but I am setting also value for key..so what in that case..? – Ranjit Jan 16 '12 at 10:46
  • I mean ,I am doing this way, [dateNoteDict setValue:notes.text forKey:datestring]; because I want to set a value for the key which I want to retrieve later – Ranjit Jan 16 '12 at 10:54
  • If you don't need to store the dictionary on disk (e.g., in your NSDefaults), then you could do: `[dateNoteDict setValue:notes.text forKey:date];`... – sergio Jan 16 '12 at 17:56
0

Set some particular date format while setting as key in your dictionary. You can use NSDateFormatter for this. Also convert new date in the same format and then use "compare" for date comparison.

Thanks

iphonedev23
  • 991
  • 2
  • 12
  • 24