问题描述
查看,我注意到它有一个文件。顾名思义,它有 NSObject
类实现,以及内置的自动释放池和保留计数实现。
Looking at the Mac OS X 10.8's version of the Objective-C runtime library source code, I noticed that it's got a NSObject.mm file. As its name suggests, it's got the NSObject
class implementation, as well as built-in autorelease pool and retain count implementations.
然而,Mountain Lion之前的ObjC运行时库的版本没有实现 NSObject
类(他们没有 NSObject .mm
文件,您可以在)。
However, versions of the ObjC runtime library prior to Mountain Lion's one didn't implement the NSObject
class (they didn't have a NSObject.mm
file, as you can see at the Mac OS X 10.7's Objective-C runtime library source code, for example).
这真的意味着 NSObject
类现在是Objective-C运行时库的一部分,而不是Foundation库组件?如果是,为什么?当子类化 NSObject
?
So does this really mean that the NSObject
class is now part of the Objective-C runtime library instead of being a Foundation library component? If yes, why? Is it to avoid one linking against the whole Foundation library (with -framework Foundation
) when subclassing NSObject
?
推荐答案
您可以使用nm(1)工具查看任何特定库的内容。
You can see what's part of any particular library using the nm(1) tool.
如果你在libobjc上运行,你会发现NSObject实际上是由libobjc提供的:
If you run this on libobjc, you'll find that NSObject is, in fact, provided by libobjc:
% nm /usr/lib/libobjc.dylib | grep -F NSObject
⋮
0000000000021688 t +[NSObject _isDeallocating]
0000000000021674 t +[NSObject _tryRetain]
0000000000021780 t +[NSObject allocWithZone:]
000000000002176e t +[NSObject alloc]
0000000000021699 t +[NSObject allowsWeakReference]
0000000000021712 t +[NSObject autorelease]
0000000000020fa6 t +[NSObject class]
000000000002115a t +[NSObject conformsToProtocol:]
00000000000217ea t +[NSObject copyWithZone:]
00000000000217e6 t +[NSObject copy]
000000000002178d t +[NSObject dealloc]
⋮
(t表示符号由此库提供;u表示符号未定义,意味着此库使用它,来自其他地方。)
("t" means that the symbol is provided by this library; "u" means that the symbol is undefined, meaning that this library uses it but it must come from somewhere else.)
这不是他们第一次移动NSObject的实现;在Lion中,你会在CoreFoundation.framework中找到它。
This isn't the first time they've moved NSObject's implementation; in Lion, you'll find it in CoreFoundation.framework.
我没有线索为什么他们移动它。无论如何,它是一个实现细节; 正式, NSObject仍然是Foundation a>。
I've got no clue why they've moved it around. At any rate, it's an implementation detail; officially, NSObject is still part of Foundation.
这篇关于NSObject类是今天Objective-C运行时库的一部分(而不是Foundation组件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!