简体   繁体   English

在Objective-C中,如何测试对象类型?

[英]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) . 对于下面的行,我们有一些结构不良的数据,可以是NSArrayNSDictionary(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.

相关问题 如何在 Objective-C 中测试类类型? - How do I test for class type in Objective-C? 如何测试 Objective-C 中的字符串是否为空? - How do I test if a string is empty in Objective-C? 如何从Swift单元测试中访问测试目标中存在的Objective-C代码? - How do I access Objective-C code that exists in the test target, from a Swift unit test? 如何使用XCTest测试navigationBar是否在Objective-C中未设置为特定颜色? - How do I use XCTest to test if a navigationBar is not set to a specific colour in objective-C? 我怎么知道一个对象是否是一个Objective-c对象,因此是由ARC处理的? - How do I know if an object is a Objective-c object and therefore is handled by ARC? 如何在Swift中使用协议公开Objective-C对象的私有类方法? - How do I expose a private class method of an Objective-C object using protocols in Swift? 如何在objective-c中保持在方法中创建的对象? - How do I keep an object created within a method alive in objective-c? 如何在objective-c中设置NSNumber变量的值(不创建新对象) - how do I set the value of an NSNumber variable (without creating a new object) in objective-c 如何将快速对象传递给javascript(UIWebView / Objective-C) - How do I pass a swift object to javascript (UIWebView / objective-C) 如何在带有NSRegularExpression的Objective-C中正确进行反向引用? - How do I do backreferences correctly in Objective-C with NSRegularExpression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM