有什么用例来定义一个新的根类? [英] What use cases are there for defining a new root class?

查看:127
本文介绍了有什么用例来定义一个新的根类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道在Objective-C中有两个主要的根类: NSObject NSProxy 。还有其他根(主要用于私人和传统目的),例如 Object NSLeafProxy

We know that in Objective-C there are two main root classes: NSObject and NSProxy. There are other roots (mainly for private and legacy purposes) like Object, and NSLeafProxy.

定义一个新的根是相当微不足道的:

Defining a new root is fairly trivial:

@interface DDRoot <NSObject>

@end

@implementation DDRoot

//implement the methods required by <NSObject>

@end

我的问题是:为什么要定义一个新的根类?是否有必要的用例?

My question is: why would you ever want to define a new root class? Is there some use-case where it's necessary?

推荐答案

据我所知,自己的根类,因为自己不能实现所有的 NSObject 协议方法,你会错过很多功能,并将做很多

As far as I can tell, there should be no reason for creating your own root class, because short of implementing all of the NSObject protocol methods yourself, you're going to be missing out on a lot of functionality, and going to be making a lot of calls to the Objective-C runtime that should essentially be done for you.

除非你真的需要实现协议不同于默认值( NSProxy 有的特殊情况),你不需要创建自己的根类。我的意思是,你必须写一个不能从根本上表示 NSObject 和Apple实现的协议的类, case,为什么你甚至在Objective-C中写它?

Unless you really had to implement the protocol differently from the default (NSProxy is a special case that does have to), you shouldn't need to make your own root class. I mean, you'd have to be writing a class that cannot fundamentally be represented by NSObject and the protocol as implemented by Apple, and in that case, why are you even writing it in Objective-C?

这就是我的想法。也许有人可以为它的创造性使用。

That's what I think. Maybe someone can come up for a creative use for it.

(研究主题的人应该看看 NSObject类引用 NSObject协议参考核心能力:根类文档,以及基础指南:Cocoa Objects文档。)

(People researching the topic should go look at the NSObject Class Reference, NSObject Protocol Reference, 'Core Competencies: Root Class' document, and the 'Root Class' section of the Fundamentals Guide: Cocoa Objects document.)

这篇关于有什么用例来定义一个新的根类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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