Re: isKindOfClass returns null instead of YES or NO
Re: isKindOfClass returns null instead of YES or NO
- Subject: Re: isKindOfClass returns null instead of YES or NO
- From: Michael Ash <email@hidden>
- Date: Thu, 21 May 2009 11:51:07 -0400
On Wed, May 20, 2009 at 11:43 AM, Jeff Decker <email@hidden> wrote:
> Hello,
> I am working through the Stanford iPhone Dev assignments along with the
> videos on iTunes. And I'm stuck on the assignment for the first week.
> Shoot!
> My goal is inspect each object of an array. When I use isKindOfClass or
> isMemberOrClass, I do not get an expected YES or NO but rather a (null).
> Here is the relevant code:
> NSMutableArray *myArray = [NSMutableArray arrayWithObjects:[NSURL
> URLWithString:@"AccountableTreeService.com"], @"Hello World",
> [NSProcessInfo processInfo], dictionary, mutableString, nil];
> NSEnumerator *enumerator = [myArray objectEnumerator];
> id step;
> while (step = [enumerator nextObject]){
> NSLog(@"========================================");
> NSLog(@"Class name: %@", [step className]);
> NSLog(@"Is Member of NSURL: %@", [[step class]
> isMemberOfClass:[NSURL class]]);
> NSLog(@"Is Kind of NSString: %@",);
> NSLog(@"Responds to LowercaseString: ");//I'm still working
> on this part
> }
In addition to what the others have mentioned, [[step class]
isKindOfClass:[NSString class]] is a nonsensical construction, as is
[[step class] isMemberOfClass:[NSURL class]]. -isKindOfClass: asks
whether the receiver (the thing you're sending it to, [step class] in
this case) is an *instance* of the class in question. In other words,
[[step class] isKindOfClass:[NSString class]] takes step's class, then
asks whether that class is an instance of NSString. The answer will
*always* be no.
You either want to write [step isKindOfClass:[NSString class]], or
write [[step class] isSubclassOfClass:[NSString class]]. They're
equivalent, but the former is less wordy and more clear.
For -isMemberOfClass:, the correct way is either [step
isMemberOfClass:[NSURL class]] or simply [step class] == [NSURL
class]. Note however that this query is rarely useful, as code should
always treat an instance of a subclass the same as an instance of a
superclass. In other words, your code shouldn't care whether 'step' is
an instance of NSURL itself or an instance of a subclass of NSURL.
Mike
_______________________________________________
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