Re: Question about respondsToSelector
Re: Question about respondsToSelector
- Subject: Re: Question about respondsToSelector
- From: Ricky Sharp <email@hidden>
- Date: Mon, 18 Aug 2008 15:27:24 -0500
On Aug 18, 2008, at 1:17 PM, Carmen Cerino Jr. wrote:
Does the id type have enough information for the respondsToSelector
method to work. I have a class with an ivar of type id, and when I
invoke the respondsToSelector method it fails when it should
succeed. I am assuming it should work fine, because if I skip
checking with the respondsToSelector method and just make the call,
it executes the method. Can someone please tell me what I am doing
wrong?
It should work just fine. This example...
NSArray* theArray = [NSArray array];
id theId = theArray;
id<NSCopying>* theCopyingId = (id<NSCopying>*) theArray;
if ([theArray respondsToSelector:@selector(isEqualToArray:)]) {
NSLog(@"theArray responds to isEqualToArray:");
} else {
NSLog(@"theArray DOES NOT respond to isEqualToArray:");
}
if ([theId respondsToSelector:@selector(isEqualToArray:)]) {
NSLog(@"theId responds to isEqualToArray:");
} else {
NSLog(@"theId DOES NOT respond to isEqualToArray:");
}
if ([(id) theCopyingId respondsToSelector:@selector(isEqualToArray:)]) {
NSLog(@"theCopyingId responds to isEqualToArray:");
} else {
NSLog(@"theCopyingId DOES NOT respond to isEqualToArray:");
}
...displays this output:
theArray responds to isEqualToArray:
theId responds to isEqualToArray:
theCopyingId responds to isEqualToArray:
Note that special casting done to remove warnings. However, without
the casting, output was the same.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden