I need to test whether the object is of type NSString
or UIImageView
. How can I accomplish this? Is there some type of "isoftype" method?
我需要测试对象是否为NSString类型或UIImageView。我怎样才能做到这一点呢?是否存在某种类型的“isoftype”方法?
6 个解决方案
#1
819
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]]
#2
53
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");
}
#3
12
When you want to differ between a superClass and the inheritedClass you can use:
当您想在超类和继承类之间有所区别时,您可以使用:
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:(类)类的aClass在这一情况下将会是正确的,因为继承类也是一种超类。
#4
11
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则没有。
#5
6
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))
#6
4
Simple, [yourobject class] it will return the class name of yourobject.
简单,[yourobject类]它将返回yourobject的类名。
#1
819
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]]
#2
53
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");
}
#3
12
When you want to differ between a superClass and the inheritedClass you can use:
当您想在超类和继承类之间有所区别时,您可以使用:
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:(类)类的aClass在这一情况下将会是正确的,因为继承类也是一种超类。
#4
11
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则没有。
#5
6
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))
#6
4
Simple, [yourobject class] it will return the class name of yourobject.
简单,[yourobject类]它将返回yourobject的类名。