Introduction to the "iOS Development series" NSObject method

Source: Internet
Author: User

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

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.