对NSString实例使用isKindOfClass:是否安全以确定类型? [英] Is it safe to use isKindOfClass: against an NSString instance to determine type?

查看:127
本文介绍了对NSString实例使用isKindOfClass:是否安全以确定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 在类集群表示的对象上使用此方法时要小心.由于类集群的性质,您返回的对象可能并不总是您所期望的类型.

然后,文档继续提供一个示例,说明为什么您永远不应该询问类似NSArray实例的内容:

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
}

现在举一个不同用途的例子,假设我有一个NSObject实例,我想在其中确定我是否有NSString或NSArray.

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.

这两种类型都是类簇-但是从上面的文档看来,危险在于isKindOfClass的答案:过于肯定(有时在您确实没有可变数组时回答YES),同时询问有关集群中的简单成员身份仍然有效.

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.

一个例子:

NSObject *originalValue;

// originalValue gets set to some instance

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

这个假设正确吗?对类集群实例使用isKindOfClass:确定成员资格真的安全吗?我对无所不在的NSString,NSArray和NSDictionary的答案特别感兴趣,但我想知道它是否可以推广.

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.

推荐答案

文档中的警告使用NSMutableArray示例.假设某些开发人员将NSMutableArray用作自己对新型数组CoolFunctioningArray的实现的基类.根据设计,此CoolFunctioningArray不可更改.但是,isKindOfClass会将YES返回到isKindOfClass:[NSMutableArray class],这是正确的,但设计上并非如此.

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:将返回YES.仅当接收方是仅作为参数传递的类的实例(即不包括子类)时,isMemberOfClass:才会返回YES.

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.

通常isKindOfClass:用于测试成员资格并不安全.如果实例为isKindOfClass:[NSString class]返回YES,则仅知道它会响应NSString类中定义的所有方法,但是您不确定将无法实现这些方法的实现.可能有人为NSString子类化了,从而引发了length方法的异常.

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.

我认为您可以使用isKindOfClass:进行这种测试,特别是如果您正在使用自己的代码(作为一名优秀的撒玛利亚人)设计的代码能够以我们都期望的方式响应的话(例如,NSString子类的length方法返回其表示的字符串的长度,而不是引发异常).如果您使用大量外部开发人员库(例如应该射击的CoolFunctioningArray的开发人员),则应谨慎使用isKindOfClass方法,最好使用isMemberOfClass:方法(可能多次以测试一组类的成员资格.

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).

这篇关于对NSString实例使用isKindOfClass:是否安全以确定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆