I have a problem with creating my own subclass of NSData
, which I want to have a custom description
method. Even creating a dummy NSData
subclass:
@interface MyData : NSData {}
@end
and
@implementation MyData
@end
and using it results in weird bugs (the function that uses it never exits, and control somehow returns to the run loop). I thought that maybe I am responsible for rewriting the designated initializers of NSData
(calling the super
implementation), but none is mentioned in the doc. So:
- what are the designated initializers of
NSData
? - what is the bare minimum I need to write for a dummy subclass of
NSData
?
Making an
NSData
subclass is difficult because (as drewag noted) it is a member of a class cluster. From the Binary Data Programming Guide:When you do
[[NSData alloc] initWith...]
you don't get back anNSData
; you probably get back anNSConcreteData
. The extraordinary Cocoa With Love has a discussion and demonstration of subclassing class clusters.The best (and most idiomatic) option is probably composition: your custom class should simply contain an
NSData
ivar, and implement a description method that operates on that enclosed object.While drewag's response is technically correct, this is a dangerous technique to use on Cocoa classes; it will override the
description
method of everyNSData
object in the program, whether you create it directly or not.In the specific case of the
description
method this may be okay, but for another method more likely to be relied upon by other objects in the framework, it could cause large, hard-to-trace problems. You should only do this if you are sure that there is no other way.It would be far better to create a category and method with a prefix:
The Apple docs specifically mention this category-override technique and advise against it:
An earlier version of the docs went on to say:
If all you want is to override a single function "description" consider using a "Category" instead:
Then, you can use this function on any instance of NSData.
It is very difficult to subclass NSData because it is a "Class Cluster." The public API treats it as one class, but in reality it is a collection of hidden subclasses. You can research overriding a class cluster, but it is almost never needed. Another option is to create your "MyData" class with NSData as a member variable instead of using a subclass.