如何使NSDate与关联对象一起工作? [英] How to make NSDate with associative object work?

查看:82
本文介绍了如何使NSDate与关联对象一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将关联对象添加到NSDate对象.为此,我使用了此处所述的方法.

I would like to add an associative object to an NSDate object. To do this, I use a method described here.

我的问题是,iOS不再(不再?)在同一日期创建不同的NSDate对象(请参见

My problem is now that iOS does not (no longer?) create different NSDate objects for the same date (see here). Therefore, if I have two items with the same NSDate, I cannot differentiate the objects and my method for adding an associative object for a particular item fails.

我尝试了不同的方法:

  • 使用NSDatecopy方法
  • NSDate *deepCopy = [[NSDate alloc] initWithTimeInterval:0 sinceDate:date];
  • NSDate *deepCopy = [date dateByAddingTimeInterval:0];
  • Using the copy method of NSDate
  • NSDate *deepCopy = [[NSDate alloc] initWithTimeInterval:0 sinceDate:date];
  • NSDate *deepCopy = [date dateByAddingTimeInterval:0];

但是我命名为deepCopy的变量不是原始date对象的深层副本.

But the variables I named deepCopy are not deep copies of the original date object.

我该怎么办?

推荐答案

您是否考虑过创建自定义模型对象来表示日期以及要与之关联的其他任何数据的组合?应尽可能避免关联对象.为什么不将NSObject子类化以创建具有两个属性的模型对象-一个NSDate属性和一个要与之关联的其他数据的一个或多个属性.

Have you thought about creating a custom model object to represent the combination of the date and whatever other data you want to associate with it? Associated objects should be avoided where possible. Why not subclass NSObject to create a model object with two properties - an NSDate property and a property/properties for the additional data you want to associate with it.

这篇关于如何使NSDate与关联对象一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆