简体   繁体   English

如果一个类继承 NSObject 会产生多少开销

[英]how much overhead is put if a class inherit NSObject

I have a lightweight c++ class needed to be ported to Objective-C to exploit the facilities provided by NSObject.我有一个轻量级的 c++ 类需要移植到 Objective-C 以利用 NSObject 提供的工具。 There will be thousands of instances of the C++ class, and i dont want to add to much overhead by wrapping it in a NSObject derived class.将有数千个 C++ 类的实例,我不想通过将它包装在 NSObject 派生类中来增加太多开销。

how big is NSObject? NSObject 有多大?

First of all, you should descend from NSObject .首先,你应该继承自NSObject There is no tangible benefit to not doing so.不这样做没有明显的好处。 NSObject adds nothing in terms of storage over the lowest level representation of a class but adds an awful lot of behaviour that will allow your classes to act correctly with the runloop, with the normal collections (eg, NSArray or NSDictionary ) and with just about everything else. NSObject在类的最低级别表示上没有增加任何存储方面的内容,但增加了大量行为,这些行为将允许您的类在运行循环、普通集合(例如, NSArrayNSDictionary )以及几乎所有内容中正确运行别的。

To answer your question specifically:具体回答你的问题:

#import <objc/runtime.h>

...

NSLog(@"obj size: %zd", class_getInstanceSize([NSObject class]));

Outputs '4' on iOS and '8' on 64-bit OS X. So the answer you're looking for is 'the natural size of an integer'.在 iOS 上输出“4”,在 64 位 OS X 上输出“8”。所以你要找的答案是“整数的自然大小”。 NSProxy is the same size, UIView (on iOS) is 84 bytes. NSProxy大小相同, UIView (在 iOS 上)为 84 字节。

Documentation for class_getInstanceSize is here — it explicitly returns "[t]he size in bytes of instances of the class cls", as the name says. class_getInstanceSize文档在这里——它显式地返回“[t]类 cls 实例的字节大小”,正如名称所说。

in the apple documentation it says here that在苹果文档中它说here

The root class of all Objective-C classes is NSObject, which is part of the Foundation framework.所有 Objective-C 类的根类是 NSObject,它是 Foundation 框架的一部分。 All objects in a Cocoa or Cocoa Touch application ultimately inherit from NSObject. Cocoa 或 Cocoa Touch 应用程序中的所有对象最终都继承自 NSObject。 This class is the primary access point whereby other classes interact with the Objective-C runtime此类是其他类与 Objective-C 运行时交互的主要访问点

So I'd say that every objective-c class ultimately derives from NSObject anyway, so you have no choice.所以我想说,无论如何,每个objective-c 类最终都是从NSObject 派生的,所以你别无选择。 Please, correct me if I'm wrong.如果我错了,请纠正我。

EDIT: What I've learned now is that NSObject is one root class , which implements the basic behaviour expected from every objective-c class.编辑:我现在学到的是 NSObject 是一个根类,它实现了每个objective-c类所期望的基本行为。 So basically you could write an objective-c class which implements the protocol of NSObject (I think that is objective-c speak for interface) and implement basic stuff like alloc and init yourself.所以基本上你可以编写一个实现 NSObject 协议的objective-c类(我认为objective-c代表接口)并自己实现allocinit类的基本东西。 Be sure to check out this thread: How to instantiate a class in Objective-C that don't inherit from NSObject请务必查看此线程: 如何在 Objective-C 中实例化不从 NSObject 继承的类

EDIT: Some more insight into root classes in objective-c , very interesting!编辑:对objective-c 中的根类有更深入的了解,非常有趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM