Common nsobject methods:
All OC objects have these methods. See the Demo code.
Reflection:
Class reflection
Instantiate an object using a string of the Class Name
Class class = nsclassfromstring (@ "student ");
Student * Stu = [[class alloc] init];
Class Name conversion to string:
Class class = [Student Class];
Nsstring * classname = nsstringfromclass (class );
Sel reflection:
Instantiate a method in the form of a method string
Sel selector = nsselectorfromstring (@ "setname :");
[STU performselector withobject: @ "Mike"];
Method to convert to string:
Nsstringfromselector (@ selector (setname :));
The test DEMO code is as follows:
# Import <Foundation/Foundation. h>
# Import "person. H"
# Import "student. H"
Void nsobject (){
Id Stu = [[STUDENT alloc] init] autorelease];
// Iskindofclass determines whether the object belongs to a class or subclass
If ([STU iskindofclass: [person class]) {
Nslog (@ "Stu belongs to person or inherits from person ");
}
// Ismemberofclass determines whether an object belongs to a certain class (excluding subclass)
Bool result = [STU ismemberofclass: [nsobject class];
Nslog (@ "% I", result );
// Directly call
[STU tests];
// Indirect call
[STU extends mselector: @ selector (tests)];
[STU initialize mselector: @ selector (Test2 :) withobject: @ "ABC"];
// Call with a latency of 2 seconds
[STU initialize mselector: @ selector (Test2 :) withobject: @ "123" afterdelay: 2];
}
Void reflect (){
// Change the string to class
Nsstring * STR = @ "student ";
Class class = nsclassfromstring (STR );
Student * Stu = [[class alloc] init];
Nslog (@ "% @", Stu );
// Change the class string
Nsstring * name = nsstringfromclass ([person class]);
Nslog (@ "Name: % @", name );
// Method reflection
Nsstring * method = @ "tests ";
Sel selector = nsselectorfromstring (method );
[STU primary mselector: Selector];
// Convert sel into a string
Nsstring * selectorname = nsstringfromselector (selector );
Nslog (@ "% @", selectorname );
[STU release];
}
Int main (INT argc, const char * argv [])
{
@ Autoreleasepool {
Nslog (@ "---- nsobject ----");
Nsobject ();
Nslog (@ "---- reflect ----");
Reflect ();
}
Return 0;
}
Person. h file:
# Import <Foundation/Foundation. h>
@ Interface person: nsobject
@ End
Person. M file:
# Import "person. H"
@ Implementation person
@ End
Student. h file;
# Import "person. H"
@ Interface Student: person
-(Void) tests;
-(Void) Test2 :( nsstring *) Param;
@ End
Student. M file:
# Import "student. H"
@ Implementation student
-(Void) tests {
Nslog (@ "excute 'test' method ");
}
-(Void) Test2 :( nsstring *) Param {
Nslog (@ "excete 'test2' method and Param is
% @ ", Param );
}
@ End