ios 在 Objective-C 中,如何测试对象类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1144629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Objective-C, how do I test the object type?
提问by James Skidmore
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”方法?
回答by mmc
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
:
同样,如果你想测试myObject
一个UIImageView
:
[myObject isKindOfClass:[UIImageView class]]
回答by Bryan Hare
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");
}
回答by Yedy
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:(Class)aClass
在这种情况下使用将导致两次都为 TRUE,因为继承的类也是一种超类。
回答by Alex Zavatone
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
没有。
回答by Bajju
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))
回答by AMohan
Simple, [yourobject class] it will return the class name of yourobject.
很简单,[yourobject class] 它将返回 yourobject 的类名。