Is There a Difference Between "Is" and Iskindofclass()

Is there a difference between is and isKindOfClass()?

Yes there is a difference: is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.

iOS 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, and only 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.

Difference between Swift is and isKindOfClass()?

I will assume that the .isKindOfClass() is the instance method from Cocoa.

  • The right side of is can be any type or protocol, whereas the argument of .isKindOfClass() must be a reference type (i.e. class). You can also test for conformance to an @objc protocol using .conformsToProtocol() instead of .isKindOfClass() in the same way.
  • The left side of is is any expression, whereas the receiver of .isKindOfClass() must be an object reference. The compiler will complain if the compile-time class of the expression is not known to support .isKindOfClass(), but you can overcome this by casting the left side to AnyObject. All Swift classes actually support .isKindOfClass() at runtime.
  • The right side of is is a type that must be hard-coded at compile-time. The argument of .isKindOfClass() can be a variable or other expression whose value is computed at runtime.

What is the difference between these inheritance checks?

There are so many link on google and specially it has been asked on stackoverflow many times, check my added link in comment and,

iPhone SDK difference between isKindOfClass and isMemberOfClass

Try reading their documentation as well, that will help you a lot.

Edited

Lets say you have a class name External and inside External class you have a subclass named Internal. I hope you have idea about class can have a subclass which can access properties of a main class. so by this method you can ask an Internal class that , Are you a subclass of External class?

[Internal isSubclassOfClass:External]

And return value will be BOOL

  • YES if the receiving class is a subclass of—or identical to—aClass,
    otherwise NO.

More reading is available on documentation website of Apple

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

isKindOfClass and NSStringFromClass disagree about UIApplicationDelegate

You haven't configured your testing target correctly. If you followed this guide for unit testing applications you should have 3 targets: Calculator, CalculatorTests and CalculatorTesting.
Check the 'build phases' section in CalculatorTests. In 'Compile Sources' only the SenTestCase source files should be listed there. I guess you added the CalculatorAppDelegate.m and other files there - this would lead to duplicate assemblies of the same source files which are then linked to the same application when you build the CalculatorTesting target. That explains why your assertions fail.

EDIT: Just realized that you don't need the CalculatorTesting target in Xcode 4. Just go to Project > Edit Schemes... and make sure the unit test bundle is listed in the Test section. Then you can run unit tests with Cmd-U.

NSDictionary (__NSDictionaryI) isMemberOfClass: isKindOfCLass: difference

Yes they are different and their difference is documented. Using isKindOfClass: will return YES for subclasses whereas isMemberOfClass: won't. Since NSDictionary is a class cluster (uses private subclasses internally) will get different results when using the two (because the instance would be a private subclass (in your case __NSDictionaryI).

When using isMemberOfClass:, this is what happens in your case:

  1. The argument item is a private dictionary subclass
  2. Evaluating isMemberOfClass: returns NO
  3. The dictionary is assigned to a UIImage variable
  4. The UIImage variable is returned (but it really contains a dictionary)
  5. You try and use that "image" and when the system asks for the image size, the dictionary doesn't respond to size and throws an exception.

Comparing of two classes

Don't use of Class do like as ..

if([childView isKindOfClass:[SFIDimmerButton class]]){

// do your stuff here ..
}

hope it help you .

NSMutableDictionary isKindOfClass NSDictionary is false

The reason is, NSDictionary.new is not guaranteed to return an NSDictionary itself - it can return a subclass of it. And your dict happens to be of a subclass like NSDictionary0. Also, the mutable one is probably of a subclass of NSMutableDictionary, like NSMutableDictionaryM.

So your NSMutableDictionary is indeed a NSDictionary, but not it's not inheriting from that other subclass.

You can easily check with isKindOf:[NSDictionary class] instead. You might want to dump or inspect the classes of your concrete objects and be surprised.

Edit: here's a quick and dirty class diagram:
Sample Image

As you can see, one is not "kind of" another, although both are NSDictionary.

This is called a "class cluster" and has some tricky implications. NSString is another famous one where the classes rarely are what newcomers expect.

isKindOfClass inexplicable identity issue

You're registering a class object, not an instance, to be passed to your callback. This is indicated by the + in the exception message.

The weirdness with isKindOfClass: is happening because [someClassObject class] returns itself, not its class (which would be a meta-class object). A class object, however, is not of its own kind (not a member of the class it represents); they have their own (opaque) hierarchy.

[[NSString class] isKindOfClass:[NSString class]]

e.g., produces the same result as your code: NO.



Related Topics



Leave a reply



Submit