首先看看兩個(gè)方法的蘋果官方解釋:
isKindOfClass:
Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)
這個(gè)方法用來(lái)判斷一個(gè)對(duì)象是否是指定類或者某個(gè)從該類繼承類的實(shí)例對(duì)象。
isMemberOfClass:
Returns a Boolean value that indicates whether the receiver is an instance of a given class. (required)
這個(gè)方法用來(lái)判斷一個(gè)對(duì)象是否是指定類的實(shí)例對(duì)象绞铃。
兩者區(qū)別:
isKindOfClass 可以判斷某對(duì)象是否是某個(gè)類的實(shí)例對(duì)象,這個(gè)類和這個(gè)類的繼承類都可以判斷荚坞;
isMemberOfClass只能判斷對(duì)象是否是當(dāng)前類的實(shí)例對(duì)象。
isMemberOfClass和isKindOfClass的應(yīng)用舉例
UIScrollView *scrollView = [[UIScrollView alloc] init];
if ([scrollView isKindOfClass:[UIView class]]) {
NSLog(@"scrollView is isKindOfClass UIView");
}
if ([scrollView isKindOfClass:[UIScrollView class]]) {
NSLog(@"scrollView is isKindOfClass UIScrollView");
}
if ([scrollView isMemberOfClass:[UIView class]]) {
NSLog(@"scrollView is isMemberOfClass UIView");
}
if ([scrollView isMemberOfClass:[UIScrollView class]]) {
NSLog(@"scrollView isMemberOfClass UIScrollView");
}
輸出結(jié)果:
scrollView is isKindOfClass UIView
scrollView is isKindOfClass UIScrollView
scrollView isMemberOfClass UIScrollView
另外需要特別注意的是NSArray颓影、NSMutableArray這樣的類:蘋果官方文檔有這樣一段描述
Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:
// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
// Modify the object
}
If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.
If the receiver is a class object, this method returns YES if aClass is a Class object of the same type, NO otherwise.
測(cè)試結(jié)果:
NSArray *testArray = [[NSArray alloc] init];
NSMutableArray *testArray2 = [[NSMutableArray alloc] init];
if ([testArray isKindOfClass:[NSArray class]]) {
NSLog(@"testArray isKindOfClass of NSArray");
}
if ([testArray isMemberOfClass:[NSArray class]]) {
NSLog(@"testArray isMemberOfClass of NSArray");
}
if ([testArray2 isKindOfClass:[NSMutableArray class]]) {
NSLog(@"testArray2 isKindOfClass of NSMutableArray");
}
if ([testArray2 isMemberOfClass:[NSMutableArray class]]) {
NSLog(@"testArray2 isMemberOfClass of NSMutableArray");
}
控制臺(tái)輸出:
testArray isKindOfClass of NSArray
testArray2 isKindOfClass of NSMutableArray
各種查詢得出的結(jié)論是:NSArray懒鉴、NSMutableArray屬于類簇,使用isMemberOfClass不能取到正確的結(jié)果咆畏。
原因是:由于類簇的性質(zhì),這類對(duì)象實(shí)際返回的實(shí)例有不確定性旧找。
NSArray對(duì)象可能會(huì)在運(yùn)行時(shí)發(fā)現(xiàn)其實(shí)運(yùn)作的是NSCFArray(來(lái)自Core Foundation框架(C語(yǔ)言的實(shí)現(xiàn)版本),很多Cocoa對(duì)象都是如此做橋接的)
總之對(duì)于類簇的判斷要謹(jǐn)慎。