Objective-c "Dynamic type Detection & Response method"

Source: Internet
Author: User

———————————————————————————————————————————
Dynamic type detection

Code:

#import <Foundation/Foundation.h>

@interface Animal:nsobject
-(void) run;
-(void) ABC;
@end

@implementation Animal
-(void) run
{
NSLog (@ "Animal run!");
}
-(void) ABC
{
NSLog (@ "abc!");
}
@end

@interface Dog:animal
-(void) run;
-(void) eat;
-(void) bark;
@end

@implementation Dog
-(void) run
{
NSLog (@ "Dog run!");
}
-(void) Eat
{
NSLog (@ "Dog eat!");
}
-(void) bark
{
NSLog (@ "Dog bark!");
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {

Dynamic Type detection:
//*******************************************************

1) Determine whether an object is an instance object of the class, or an instance object (object and Class) of its child class
Iskindofclass use format: [Object Iskindofclass: Class object];

Animal *ani=[animal New];

BOOL Isistance=[ani Iskindofclass:[ani class]];
BOOL Isinstance=[ani Iskindofclass:[animal class]];
Both of the above are fine, because you can invoke the class object with the instance object/class name of the class type

NSLog (@ "isinstance =%d", isinstance);//output is 1 (ANI is an instance object of animal, naturally 1)


Dog *dog=[dog New];
BOOL Isinstance1=[dog Iskindofclass:[animal class]];
NSLog (@ "IsIstance1 =%d", isInstance1);//output is 1 (dog is a subclass of animal, dog is an instance object of dog, naturally 1)


Person *p=[person new];
BOOL isinstance2=[p Iskindofclass:[animal class]];
NSLog (@ "IsInstance2 =%d", isInstance2);//The output is 0 (person is an unrelated class, not a subclass of animal, p is an instance object of person, so 0)

//*******************************************************

2) Determine whether an object is an instance object of the class (single-finger, excluding its subclasses)

Ismemberofclass use format: [Object Ismemberofclass: Class object];

BOOL Isinstance3=[ani Ismemberofclass:[animal class]];
NSLog (@ "IsInstance3 =%d", isInstance3);//output is 1 (ANI is an instance object of animal, naturally 1)

BOOL Isinstance4=[dog Ismemberofclass:[animal class]];
NSLog (@ "IsInstance4 =%d", isInstance4);//output is 0 (dog is an instance object of dog, dog is a subclass of animal, so 0)

//*******************************************************

3) Determine if a class is a subclass of another class

Issubclassofclass Use format: [Class Name/Class object Issubclassofclass: Class object];

BOOL Isinstance5=[dog Issubclassofclass:[animal class]];
NSLog (@ "IsInstance5 =%d", isInstance5);//output is 1 (dog is a subclass of animal)

BOOL isinstance6=[animal Issubclassofclass:[dog class]];
NSLog (@ "IsInstance6 =%d", isInstance6);//output is 0 (animal is the parent of dog)

BOOL Isinstance7=[[dog class] Issubclassofclass:[animal class]];
NSLog (@ "IsInstance7 =%d", isInstance7);//output is 1 (note here the form of the class object before you can write)

BOOL Isinstance8=[[dog class] issubclassofclass:animal];//This sentence compilation does not pass because the Animal is not the format of the class object

//*******************************************************

4) Determine if the object can respond to the specified method

Respondstoselector use format: [Object Respondstoselector: Sel of Method];
BOOL Isrespond1=ani respondstoselector:<# (SEL) #>//Here Obviously the parameter passed in should be a SEL type
Let's review what SEL is. First the SEL represents the storage location of the method, we typically wrap the method as SEL type, then find the address of the method based on the SEL data, and then call the appropriate method based on the method address. So the next thing we should do:

sel [email protected] (eat);//first encapsulate the data into SEL type, get the address of the method
BOOL Isrespond1=[dog Respondstoselector:s1];//eat is the method in dog, dog is the instance object of dog, so you can access
NSLog (@ "IsRespond1 =%d", isRespond1);//output is 1

SEL [email protected] (bark);
BOOL Isrespond2=[ani Respondstoselector:s2];//ani is an instance object of animal, but bark is a unique method of dog, so it cannot be accessed
NSLog (@ "Isrespond =%d", isRespond2);//output is 0

So, we generally use the instance object to invoke the method before, you can make a judgment, as follows:
if (ISRESPOND1)
//        {
[Dog Eat];
//        }
Else
//        {
NSLog (@ "cannot be called");
//        }
This will kill the error at compile time, rather than the error found when running.

//*******************************************************

5) Determine whether the class can invoke (corresponding) the specified method
BOOL Isrespond3=[dog instancesrespondtoselector:s1];//s1 is a eat sel package, eat is the method of Dog, so you can call
NSLog (@ "IsRespond3 =%d", isRespond3);//output is 1

BOOL Isrespond4=[animal instancesrespondtoselector:s1];//Obviously Animal cannot invoke its subclass-specific methods
NSLog (@ "IsRespond4 =%d", isRespond4);//output is 0

SEL [email protected] (ABC);//abc method is animal in the
BOOL Isrespond5=[dog instancesrespondtoselector:s3];//Subclass inherits the ABC method of the parent class, the natural subclass can be called (★ Here I want to illustrate a point, if you write only the declaration of ABC method in the parent class, Without writing the implementation, the result here is 0, which means that you cannot invoke ★)
NSLog (@ "IsRespond5 =%d", isRespond5);//output is 1

//*******************************************************

}
return 0;
}


———————————————————————————————————————————
Response method (belongs to the dynamic Type Detection section)

#import <Foundation/Foundation.h>

@interface Animal:nsobject
-(void) run;
@end

@implementation Animal
-(void) run
{
NSLog (@ "Animal run!");
}
@end

@interface Dog:animal
-(void) Eat: (NSString *) foodname;
-(void) Eat: (NSString *) foodname anddogname: (NSString *) dogname;
@end

@implementation Dog
-(void) Eat: (NSString *) foodname
{
NSLog (@ "Dog eat%@", foodname);
}
-(void) Eat: (NSString *) foodname anddogname: (NSString *) dogname
{
NSLog (@ "%@ eat%@", dogname,foodname);
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
Call methods later, mostly in this form
When calling a method, we should have this idea and develop a writing habit:
① first call the method to have an instance object, so create the instance object first
② Next Call the method to determine whether it can be called, so there must be a judgment statement (whether the object can invoke the method)
③ This method is called in SEL format because the object can invoke the method, so the method is first converted to the Sel form.
④ then the judgment ends, the return value is 1 on the call, the return value is 0 on the output cannot be called
⑤ Last Call to the no-parameter/parameter method (what is covered in this section)
//*******************************************************
Animal *ani =[[animal Alloc]init];//①
SEL [email protected] (run);//③
BOOL Isrespond=[ani Respondstoselector:s1];//②
if (Isrespond) {//④
[ANI Performselector:s1];//⑤ instance object calls no argument method
}
Else
{
NSLog (@ "cannot be called!");
}
//*******************************************************
Dog *dog=[[dog Alloc]init];
SEL [email protected] (EAT:);//When you get a method address with multiple parameters, just write the method name
BOOL Isrespond2=[dog RESPONDSTOSELECTOR:S2];
if (IsRespond2) {
[Dog performselector:s2 withobject:@ "Coffee"];//instance object calls a method that contains a parameter
}
Else
{
NSLog (@ "cannot be called!");
}
//*******************************************************
Dog *dog2=[[dog Alloc]init];
SEL [email protected] (eat:anddogname:);//write Method name only
BOOL Isrespond3=[dog RESPONDSTOSELECTOR:S3];
if (IsRespond3) {
[Dog Performselector:s3 withobject:@ "hotdog" withobject:@ "Bigmax"];//instance object calls a method with two parameters
}
Else
{
NSLog (@ "cannot be called!");
}
//*******************************************************
}
return 0;
}


———————————————————————————————————————————

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c "Dynamic type Detection & Response method"

Related Article

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.