Introduction to the "iOS Development series" NSObject method

Source: Internet
Author: User

NSObject is the base class in OC, 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 useful 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" infers whether student is an object of the person class

-(BOOL) Ismemberofclass: (Class) AClass;        [Student Ismemberofclass:[person class]];

"2" infers whether student is an object of the person class or subclass
-(BOOL) Iskindofclass: (Class) AClass;        [Student Iskindofclass:[person class]];

"3" infers whether student follows the MyProtocol protocol (and can also invoke the class to infer 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" infers 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" indirect call to Student's Test1 method (test1 no parameters)
-(ID) Performselector: (SEL) aselector;        [Student Performselector: @selector (test1)];

the "6" indirectly calls the Test2 method of student (Test2 has a number of parameters)
-(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, since the command line has finished running out of the main function, the iOS part of the main function has been running.) 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>

Introduction to the "iOS Development series" NSObject method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.