Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

objective c - Is it safe to use isKindOfClass: against an NSString instance to determine type?

From the isKindOfClass: method documentation in NSObject:

Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected.

The documentation then proceeds to give an example of why you should never ask something like the following of an NSArray instance:

// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
    // Modify the object
}

Now to give an example of a different use, let's say I have an instance of NSObject where I would like to determine if I have an NSString or NSArray.

Both of these types are class clusters - but it seems from the documentation above that the danger lies in the answer to isKindOfClass: being too affirmative (answering YES sometimes when you really do not have a mutable array) whereas asking a question about simple membership in a cluster would still be valid.

An example:

NSObject *originalValue;

// originalValue gets set to some instance

if ( [originalValue isKindOfClass:[NSString class]] )
   // Do something with string

Is this assumption correct? Is it really safe to use isKindOfClass: against class cluster instances to determine membership? I'm specifically interested in the answer for the omnipresent NSString, NSArray and NSDictionary but I'd be interested to know if it's generalizable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The warning in the documentation uses the NSMutableArray example. Suppose some developer uses NSMutableArray as base class for his own implementation of a new kind of array CoolFunctioningArray. This CoolFunctioningArray by design is not mutable. However, isKindOfClass will return YES to isKindOfClass:[NSMutableArray class], which is true, but by design is not.

isKindOfClass: will return YES if the receiver somewhere inherits from the class passed as argument. isMemberOfClass: will return YES only if the receiver is an instance of the class passed as argument only, i.e. not including subclasses.

Generally isKindOfClass: is not safe to use to test for membership. If an instance returns YES for isKindOfClass:[NSString class], you know only that it will respond to all methods defined in the NSString class, but you will not know for sure what the implementation of those methods might do. Someone might have subclassed NSString to raise an exception for the length method.

I think you could use isKindOfClass: for this kind of testing, especially if you're working with your own code which you (as a good Samaritan) designed in such a way it will respond in a way we all expect it to (e.g. the length method of an NSString subclass to return the length of the string it represents instead of raising an exception). If you're using a lot of external libraries of weird developers (such as the developer of the CoolFunctioningArray, which should be shot) you should use the isKindOfClass method with caution, and preferably use the isMemberOfClass: method (possibly multiple times to test membership of a group of classes).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...