创建和使用虚拟NSData子类不起作用 [英] Creating and using a dummy NSData subclass doesn't work

查看:93
本文介绍了创建和使用虚拟NSData子类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,创建我自己的子类 NSData ,我想有一个自定义描述方法。甚至创建一个 NSData 子类:

  @interface MyData:NSData { } 
@end

  @implementation MyData 
@end

使用它会导致奇怪的错误(使用它的函数永远不会退出,并且控制以某种方式返回到运行循环)。我认为也许我负责重写 NSData (调用 super 实现)的指定初始化器,但没有在文档中提到。所以: NSData ? $ <$ p


  • b $ b
  • 为什么需要为 NSData


解决方案

创建一个 NSData 子类很困难,因为(as drewag注意到)它是 class cluster a>。从二进制数据编程指南


...数据对象不是NSData或NSMutableData类的实际实例,


当你执行 [[NSData alloc] initWith .. 。] 你不会得到一个 NSData ;你可能会得到一个 NSConcreteData 。非凡的Cocoa With Love有一个讨论和演示子类类集群。



最好的(最常用的)选项可能是 composition :你的自定义类应该只包含一个 NSData ivar,并实现对封闭对象操作的描述方法。



虽然drewag的响应在技术上是正确的,但这是一个危险的技术,类;它将覆盖程序中每个 NSData 对象的<$ c $>描述方法,无论是创建它直接或不。



描述方法的特定情况下,这可能是好的,但对于另一种更可能被依赖的方法在框架中的其他对象,它可能导致大的,难以追踪的问题。你应该这样做,如果你确定没有其他方式。



更好的方法是创建一个带有前缀的类别和方法:

  @interface NSData(FX_Description)
- (NSString *)FX_description;
@end

Apple文档具体提及此类别 - 覆盖技术并针对它提供建议:


由于类别中声明的方法已添加到现有类中,因此您需要非常小心方法名称。



如果类别中声明的方法名称与原始类别中的方法相同,或者同一类别上的另一个类别中的方法或者甚至超类),该行为是未定义的在运行时使用哪个方法实现。


早期版本的文档可以说:


某些类别方法的存在可能会导致所有框架的行为更改。例如,如果在 NSObject 的类别中覆盖 windowWillClose:委托方法,所有窗口委托您的程序然后使用类别方法响应;所有您的实例 NSWindow 的行为可能会改变。您在框架类上添加的类别可能会导致神秘的行为变化并导致崩溃。 [强调我。]



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:

...data objects are not actual instances of the NSData or NSMutableData classes but instead are instances of one of their private subclasses.

When you do [[NSData alloc] initWith...] you don't get back an NSData; you probably get back an NSConcreteData. 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 every NSData 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:

@interface NSData (FX_Description) 
- (NSString *)FX_description;
@end

The Apple docs specifically mention this category-override technique and advise against it:

Because the methods declared in a category are added to an existing class, you need to be very careful about method names.

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.

An earlier version of the docs went on to say:

The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances of NSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes. [Emphasis mine.]

这篇关于创建和使用虚拟NSData子类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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