objective c - Why compiler doesn't return error on [NSObject init] -
when try compile following code:
int main(int argc, const char * argv[]) { @autoreleasepool { [nsobject init]; } return 0; }
compiler let build , app crashes in runtime following exception:
2017-08-14 14:56:07.937859-0700 nsobjectinit[30512:11241814] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** +[nsobject<0x7fff9fd83140> init]: cannot init class object.' *** first throw call stack: ( 0 corefoundation 0x00007fff818e32cb __exceptionpreprocess + 171 1 libobjc.a.dylib 0x00007fff966f348d objc_exception_throw + 48 2 corefoundation 0x00007fff8196517f +[nsobject(nsobject) init] + 127 3 nsobjectinit 0x0000000100000f2d main + 61 4 libdyld.dylib 0x00007fff96fd9235 start + 1 5 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception
as understand, there +init method in nsobject class, didn't find in public headers , if private, complier should tell there no such method. ideas why compiler let build code?
classes objects. there implicit "meta-class" each class , class 1 , instance of meta-class. think of "class method on class foo" instance method on class meta-foo.
in further quirk, meta-class of root class of class hierarchy subclass of root class itself. meta-nsobject
inherits nsobject
. class nsobject
instance of itself!
so, instance method of nsobject
class method of nsobject
. is, can invoked on nsobject
class itself. that's why compiler doesn't have problem [nsobject init]
.
http://sealiesoftware.com/blog/archive/2009/04/14/objc_explain_classes_and_metaclasses.html
Comments
Post a Comment