[英]In Objective-C, how do I test the object type?
I need to test whether the object is of type NSString
or UIImageView
. 我需要测试对象是
NSString
类型还是UIImageView
类型。 How can I accomplish this? 我怎么能做到这一点? Is there some type of "isoftype" method?
是否有某种类型的“isoftype”方法?
If your object is myObject
, and you want to test to see if it is an NSString
, the code would be: 如果你的对象是
myObject
,并且你想测试它是否是NSString
,那么代码将是:
[myObject isKindOfClass:[NSString class]]
Likewise, if you wanted to test myObject
for a UIImageView
: 同样,如果您想为
UIImageView
测试myObject
:
[myObject isKindOfClass:[UIImageView class]]
You would probably use 你可能会用
- (BOOL)isKindOfClass:(Class)aClass
This is a method of NSObject
. 这是
NSObject
一种方法。
For more info check the NSObject
documentation. 有关更多信息,请查看
NSObject
文档。
This is how you use this. 这就是你如何使用它。
BOOL test = [self isKindOfClass:[SomeClass class]];
You might also try doing somthing like this 你也可以尝试做这样的事情
for(id element in myArray)
{
NSLog(@"=======================================");
NSLog(@"Is of type: %@", [element className]);
NSLog(@"Is of type NSString?: %@", ([[element className] isMemberOfClass:[NSString class]])? @"Yes" : @"No");
NSLog(@"Is a kind of NSString: %@", ([[element classForCoder] isSubclassOfClass:[NSString class]])? @"Yes" : @"No");
}
When you want to differ between a superClass and the inheritedClass you can use: 如果想要在superClass和inheritedClass之间有所不同,可以使用:
if([myTestClass class] == [myInheritedClass class]){
NSLog(@"I'm the inheritedClass);
}
if([myTestClass class] == [mySuperClass class]){
NSLog(@"I'm the superClass);
}
Using - (BOOL)isKindOfClass:(Class)aClass
in this case would result in TRUE both times because the inheritedClass is also a kind of the superClass. 使用
- (BOOL)isKindOfClass:(Class)aClass
在这种情况下, - (BOOL)isKindOfClass:(Class)aClass
将导致TRUE两次,因为inheritedClass也是一种superClass。
Running a simple test, I thought I'd document what works and what doesn't. 运行一个简单的测试,我想我会记录哪些有效,哪些无效。 Often I see people checking to see if the object's class is a member of the other class or is equal to the other class.
通常我会看到人们检查对象的类是否是另一个类的成员或者是否等于另一个类。
For the line below, we have some poorly formed data that can be an NSArray
, an NSDictionary
or (null)
. 对于下面的行,我们有一些结构不良的数据,可以是
NSArray
, NSDictionary
或(null)
。
NSArray *hits = [[[myXML objectForKey: @"Answer"] objectForKey: @"hits"] objectForKey: @"Hit"];
These are the tests that were performed: 这些是执行的测试:
NSLog(@"%@", [hits class]);
if ([hits isMemberOfClass:[NSMutableArray class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isMemberOfClass:[NSMutableDictionary class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isMemberOfClass:[NSArray class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isMemberOfClass:[NSDictionary class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isKindOfClass:[NSMutableDictionary class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isKindOfClass:[NSDictionary class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isKindOfClass:[NSArray class]]) {
NSLog(@"%@", [hits class]);
}
if ([hits isKindOfClass:[NSMutableArray class]]) {
NSLog(@"%@", [hits class]);
}
isKindOfClass
worked rather well while isMemberOfClass
didn't. isKindOfClass
运行得相当好,而isMemberOfClass
却没有。
You can make use of the following code incase you want to check the types of primitive data types. 如果要检查原始数据类型的类型,可以使用以下代码。
// Returns 0 if the object type is equal to double
strcmp([myNumber objCType], @encode(double))
简单,[yourobject类]它将返回yourobject的类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.