NSDate 作为 NSDictionary 的键
是否可以将 NSDate 添加为键,将一些数组添加为 NSDictionary 中的值?
我没有任何将字典写入磁盘的要求 - 存档或取消存档,就像这个人需要它一样: NSDictionary 以 NSDates 作为键
但是,为什么我希望将 NSDate 添加为键,这样我就可以从字典中获取所有键并进行一些计算,例如获取日期从一个范围内。
具有相同日期值的两个对象在创建时是否共享相同的内存?
是否可以将 NSDate 作为密钥?另外,这个假设我可能会面临哪些其他问题?
谢谢,
拉吉
编辑: 发布我的问题后,我只是编写了一个示例代码:
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);
输出:
Date 1 = 7945610, Date 2 = bd03610
但是 NSDictionary 的关键比较是否将这 2 个 NSDate 对象解释为不同的?
另一个编辑:
另外,如果我应该使用 NSDateFormatter 将 NSDate 转换为 NSString,我无法直接检查范围内的日期。我必须执行以下步骤:
- 从字典中获取所有 NSString 键数组
- 将它们转换回 NSDate 数组
- 执行谓词并获取范围内的日期
- 再次将这些日期转换回字符串数组
- 现在使用此字符串键从中获取值字典!
那么,有没有更好的办法呢?
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:
- Get all NSString array of keys from dictionary
- Convert them back to array of NSDate
- Perform predicates and fetch dates within range
- Again convert back those dates to an array of Strings
- Now use this string keys to get values from dictionary!
So, is there any better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,
NSDate
对象可以用作NSDictionary
的键 - 任何对象类型都可以用作键,只要它支持NSCopying
协议。键通过指针值使用isEqual:
和 not 进行比较。有关更多详细信息,请参阅 NSDictionary 文档的概述部分。Yes,
NSDate
objects can be used as keys forNSDictionary
- any object type can be used as a key provided it supports theNSCopying
protocol. Keys are compared usingisEqual:
and not by pointer value. See the Overview section of theNSDictionary
documentation for more details.