iPhone SDK difference between isKindOfClass and isMemberOfClass
Can any one tell me what the difference is between the
isKindOfClass:(Class)aClass and the
I know it is something small like, one is global while the other is an exact class match but I need someone to specify which is which please.
- What is the difference between UIView and UIViewController?
- Why Choose Struct Over Class?
- How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?
- A method without parameters is calling for an argument
- Why can't Swift initializers call convenience initializers on their superclass?
- Swift: Test class type in switch statement
6 Solutions Collect From Internet About “iPhone SDK difference between isKindOfClass and isMemberOfClass”
isKindOfClass: returns YES if the receiver is an instance of the specified class or an instance of any class that inherits from the specified class.
isMemberOfClass: returns YES if the receiver is an instance of the specified class.
Most of the time you want to use
isKindOfClass: to ensure that your code also works with subclasses.
The NSObject Protocol Reference talks a little more about these methods.
isKindOfClass:indicates whether an object inherits from a given class
isMemberOfClass:indicates whether an object is an instance of a given class.
[[NSMutableData data] isKindOfClass:[NSData class]]; // YES [[NSMutableData data] isMemberOfClass:[NSData class]]; // NO
@interface A : NSObject @end @interface B : A @end ... id b = [[B alloc] init];
[b isKindOfClass:[A class]] == YES; [b isMemberOfClass:[A class]] == NO;
-isMemberOfClass: is true if the instance is exactly of the specified class, while
-isKindOfClass: is true if the instance is exactly of the specified class or if one of the instance’s ancestors is of the specified class.
-isMemberOfClass: is seldom used.
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.
isMemberOfClass: Returns a Boolean value that indicates whether the
receiver is an instance of a given class.
isKindOfClass-> return YES when the object is instance of that class or instance of a class which is inherited from it.
isMemberOfClass: return YES when the object is instance of that class but No in case: instance of a class which is inherited from it.
example is good enough in jtbandes answer.
Because of class clusters, isMemberOfClass can give you an answer you might not expect. In many cases your best choice is more likely to be -(BOOL)conformsToProtocol:(SEL)aSelector or – (BOOL)conformsToProtocol:(Protocol*)aProtocol. I.e, it’s better to test these if they can answer your need rather than testing class/subclass.
See apple doc for NSObject class and protocol:
- How to apply filter to Video real-time using Swift
- Swift UITableView delegate and dataSource declaration and retain cycles
- Detect volume button press
- How to create framework from Xcode 4 project?
- How to set up SceneKit collision detection
- Anyway to get string from variable name?
- Is that possible to use UIPageControl to control the move of UITableView?
- iOS: Drawing just portions of a UImage
- About ATS SSL in iOS 9 release version
- iOS 6.0 AutoLayout issue with constraints: subviews disappear when rotated
- What is the Objective-C equivalent for “toString()”, for use with NSLog?
- Designing inside a scrollview in xcode 4.2 with storyboards
- NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why？
- Why can't I use the new inline #imageLiteral syntax in Xcode 8? “Expected expression in return result”
- API to get a provisioning profile from Apple dev site