2012-02-27 70 views

回答

2

所有類別的NSObject的類繼承有幾個方法來測試繼承,行爲和一致性:

– isKindOfClass: 
– isMemberOfClass: 
– respondsToSelector: 
– conformsToProtocol: 

在你的情況下使用isKindOfClass:方法:

if ([someObject isKindOfClass:[NSString class]]) {...} 
if ([someObject isKindOfClass:[NSDictionary class]]) {...} 
0

isKindOfClass:消息是你的朋友:

id value = [dict [email protected]"foo"]; 

BOOL isString = [value isKindOfClass:[NSString class]]; 
BOOL isDict = [value iskindOfClass:[NSDictionary class]]; 

NSLog(@"is string:%i\tis dictionary:%i", isString, isDict); 

參考資料:NSObject Protocol Reference

5

你要測試使用isKindOfClass。

例如:

 
NSObject *object = [dict objectForKey:@:"key"]; 
if ([object isKindOfClass:[NSString class]]) 
{ 
    // it's a string 
} 
else if ([object isKindOfClass:[NSDictionary class]]) 
{ 
    // it's a dictionary 
} 
相關問題