NSObject is the base class in OC, and all classes inherit from this, which also gives us a lot of methods related to "class" and "method", this article will explain a few very practical methods.
Text:
Person.h
#import <Foundation/Foundation.h> @interface person:nsobject @end </span>
Student.h
#import "Person.h"//Inherit person class @interface Student:person-(void) test1; -(void) Test2: (NSString *) string; @end </span>
MyProtocol.h
#import <Foundation/Foundation.h> @protocol myprotocol @end </span>
"1" Determines whether student is an object of the person class
-(BOOL) Ismemberofclass: (Class) AClass; [Student Ismemberofclass:[person class]];
"2" Determines whether student is an object of the person class or subclass
-(BOOL) Iskindofclass: (Class) AClass; [Student Iskindofclass:[person class]];
"3" Determines whether student follows the MyProtocol protocol (or it can be invoked with a class to determine if the class follows)
-(BOOL) Conformstoprotocol: (Protocol *) Aprotocol; [Student Conformstoprotocol: @protocol (MyProtocol)]; Or use the class method //+ (BOOL) Conformstoprotocol: (Protocol *) Protocol; [Student Conformstoprotocol: @protocol (MyProtocol)];
"4" Determines whether the Test1 method of the student responds (that is, whether the Test1 method is declared and implemented)
-(BOOL) Respondstoselector: (SEL) aselector; [Student Respondstoselector: @selector (test1)];
"5" indirectly calls Student's Test1 method (test1 no parameters)
-(ID) Performselector: (SEL) aselector; [Student Performselector: @selector (test1)];
"6" indirectly calls Student's Test2 method (Test2 has one parameter)
-(ID) Performselector: (SEL) Aselector withobject: (ID) object; [Student Performselector: @selector (test2:) withobject:@ "123"]; With a maximum of two parameters //-(ID) performselector: (SEL) Aselector withobject: (ID) object1 withobject: (ID) object2;
"7" Delay 2s call test1 method of student
(There is no delay effect on the command line because the command line exits the main function after execution, and in the iOS section the main function is always executing, so you can see the delay effect)
<span style= "Font-family:simhei;" >//-(void) Performselector: (SEL) Aselector withobject: (ID) anargument afterdelay: (nstimeinterval) delay; Delay unit is (seconds) [student Performselector: @selector (test2:) withobject:@ "123" afterdelay:2];</span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to the "iOS Development series" NSObject method