1

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

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.

I've tried different approaches:

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

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

What can I do?

Community
  • 1
  • 1
John
  • 8,468
  • 5
  • 36
  • 61
  • 1
    usually the when NS does tricky stuff you can get a CF plain C version that doesn't do that tricky stuff, or that you can turn off at least... https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFDateRef/index.html#//apple_ref/c/tdef/CFDateRef – Grady Player May 18 '15 at 16:14
  • I think that would also be a solution. However, I still hope that someone has another idea that would mean fewer changes in my current project. :-) – John May 18 '15 at 16:23
  • 3
    The problem is that the 64-bit runtime uses "tagged pointers" in many cases (where the pointer is not a pointer but contains the information directly). Unfortunately, associated objects do not work with tagged pointers, this was also observed here: http://stackoverflow.com/questions/21561211/objc-setassociatedobject-function-error-in-64bit-mode-not-in-32bit. – Martin R May 18 '15 at 16:41
  • @Martin R: Thank you! Now it's definite: I have to change my code. – John May 18 '15 at 16:44
  • 2
    Even under the best of circumstances the Objective-C "associative object" mechanism is fragile. It's best to avoid it. – Hot Licks May 19 '15 at 01:04

1 Answers1

3

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.

johnpatrickmorgan
  • 2,372
  • 2
  • 13
  • 17
  • Thank you. Yes, that would be a better way than using associative objects. And I would use this if I cannot make my current approach work. However, it would mean that I'd have to make many changes in my current code... – John May 18 '15 at 16:21
  • 4
    Don't get me wrong, but this is the result of relying on undocumented behavior (`-copy`) and edge case solutions (associated objects). – Amin Negm-Awad May 18 '15 at 16:26
  • You are absolutely right. I very much regret that I decided to use associative objects. – John May 18 '15 at 16:27
  • @johnpatrickmorgan: A thought just crossed my mind: Wouldn't it be better to subclass NSDate directly instead of NSObject? – John May 18 '15 at 16:29
  • 1
    @vomako I'd say that depends on what information you're associating with the date object, but probably not. If it's not inherently date-related information, then the approach I suggested is likely more appropriate. – johnpatrickmorgan May 18 '15 at 16:36
  • @johnpatrickmorgan: Thank you. I realize that I've never mentioned what I wanted to achieve. :-) I'm currently associating a variable "allDay" with it. – John May 18 '15 at 16:38
  • 3
    NSDate is a class cluster, therefore subclassing it is going to be difficult :) – Martin R May 18 '15 at 16:40