I have code that gets a TableViewCell using the cellForIndexPath API. I am using storyboards and expecting the cell to be custom type of EditItemTableViewCell. This class inherits from UITableViewCell.
The debugger (and all I can find) says my variable is a EditItemTableViewCell, yet isKindOfClass says my cell is not an EditItemTableViewCell. Here is some code I put together to test what is going on:
BOOL bCellOK = [cell isKindOfClass:[EditItemTableViewCell class]];
NSString *strCellClass = NSStringFromClass([cell class]);
NSString *strReferenceClass = NSStringFromClass([EditItemTableViewCell class]);
NSLog(#"cell class is %#",strCellClass);
NSLog(#"desired class is %#",strReferenceClass);
if ([strCellClass isEqualToString:strReferenceClass])
NSLog(#"Both the cell class and the reference class are the same, so bCellOK should be YES. The value is %#", bCellOK ? #"YES" : #" NO");
else
NSLog(#"The cell class and the reference class are the different, so bCellOK should be NO. The value is %#", bCellOK ? #"YES" : #" NO");
The debugger output shows:
2021-11-21 13:56:26.039446-0500 XXXXXXXXXXX[21961:3210758] cell class is EditItemTableViewCell
2021-11-21 13:56:26.039564-0500 XXXXXXXXXXX[21961:3210758] desired class is EditItemTableViewCell
2021-11-21 13:56:26.039598-0500 XXXXXXXXXXX[21961:3210758] Both the cell class and the reference class are the same, so bCellOK should be YES. The value is NO
Xcode is version 13.1 (13A1030d)
I have successfully used isKindOfClass in the past. Am I doing something wrong? Any and all help appreciated.
Related
I've been trying to get this script print out the one of the two types of object that was inserted. However, it always prints both types while only one thing is inserted. There are two classes, VKJItem and VKJBox. VKJBox is a subclass of VKJItem.
This is the main.m:
VKJBox *box1 = [[VKJBox alloc] init];
VKJBox *box2 = [[VKJBox alloc] init];
[box1 addItem:box2];
and this is the implementation of my VKJBox's addItem method:
if ([item isKindOfClass:[VKJBox class]]) {
NSLog(#"BOX");
}
if ([item isKindOfClass:[VKJItem class]]) {
NSLog(#"ITEM");
}
The problem is that the script prints both BOX and ITEM to the console.
VKJBox is a subclass of of VKJItem and therefore VKJBox is a kind of VKJItem and a kind of VKJBox.
-isKindOfClass: is used to determine whether an object is an instance of a class or an instance of a class which inherits from the class.
For example:
isKindOfClass[UIView class] will be true for UIImageView, UILabel, etc.
Use the -isMemberOfClass: to check if the object is an instance of the specified class exactly.
On of your Classes must be a subclass of Other.
isKindClass:
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.
You need to use isMemberOfClass
Source
#interface testing
#property NSString *email;
#property NSString *password;
#end
In another class , how to get data type of email, or password without defined the email and password.
NSArray *keys=[[NSArray alloc] init];
[keys addObject:#"email"];
testing *detail=[[testing alloc]init];
if([[detail valueForKey:keys[0]] isKindOfClass:[NSString class]])
{ NSLog("This is string class variable"); }
but it not enter the if condition
All you need to do to obtain the class of a property is
[email class];
[password class];
There are ways to test the object class as well using isKindOfClass: and isMemberOfClass: and even isSubclassOfClass:
You can use these in if statements like
if([email isKindOfClass:[NSString class]])
and/or
if([password isMemberOfClass:[NSString class]])
and/or
if([[email class] isSubclassOfClass:[NSString class]])
For a description of each these please read the Apple Documentation on NSObject class reference. and Apple Documentation on NSObject protocol reference
isSubclassOfClass:
Returns a Boolean value that indicates whether the receiving class is a subclass of, or identical to, a given class.
+ (BOOL)isSubclassOfClass:(Class)aClass
Parameters
aClass
A class object.
Return Value
YES if the receiving class is a subclass of—or identical to—aClass, otherwise NO.
Availability
Available in iOS 2.0 and later.
Declared In
objc/NSObject.h
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)
- (BOOL)isKindOfClass:(Class)aClass
Parameters
aClass
A class object representing the Objective-C class to be tested.
Return Value
YES if the receiver is an instance of aClass or an instance of any class that inherits from aClass, otherwise NO.
Discussion
For example, in this code, isKindOfClass: would return YES because, in Foundation, the NSArchiver class inherits from NSCoder:
NSMutableData *myData = [NSMutableData dataWithCapacity:30];
id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
if ( [anArchiver isKindOfClass:[NSCoder class]] )
...
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.
Availability
Available in OS X v10.0 and later.
See Also
– isMemberOfClass:
Related Sample Code
QuickLookSketch
Sketch
Sketch+Accessibility
Declared In
objc/NSObject.h
isMemberOfClass:
Returns a Boolean value that indicates whether the receiver is an instance of a given class. (required)
- (BOOL)isMemberOfClass:(Class)aClass
Parameters
aClass
A class object representing the Objective-C class to be tested.
Return Value
YES if the receiver is an instance of aClass, otherwise NO.
Discussion
For example, in this code, isMemberOfClass: would return NO:
NSMutableData *myData = [NSMutableData dataWithCapacity:30];
id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
if ([anArchiver isMemberOfClass:[NSCoder class]])
...
Class objects may be compiler-created objects but they still support the concept of membership. Thus, you can use this method to verify that the receiver is a specific Class object.
Availability
Available in OS X v10.0 and later.
See Also
– isKindOfClass:
Declared In
objc/NSObject.h
EDIT
When trying to access these properties in another class (Which isn't what you are asking in your question) do the following.
Testing *testingClassIVar = [[Testing alloc] init];
[testingClassIVar email];
[testingClassIVar password];
// to get the class type of these
[[testingClassIVar email] class];
[[testingClassIVar password] class];
EDIT 2
OMG just NO I don't even know what you are trying to do but it seems very wrong. NSArray doesn't respond to addObject: so this will crash your code. If you want to use addObject: then change the NSArray to NSMutableArray but unless you plan on using that array in a loop (If you are please specify I will not assume) then just access the property by using [detail email]
Change to this
testing *detail= [[testing alloc] init];
if([[detail email] isKindOfClass:[NSString class]]) {
NSLog("This is string class variable");
}
I want to get the class name of an object as what we are using.
That means now if I write this code
NSString *s = [NSString string];
NSLog(#"%#",[s class]);
The output is __NSCFConstantString
How can I get it as NSString itself ?
Note : NSString is just an example
I know __NSCFConstantString is correct. But my intention is to get like NSString. Is there any way to acheive this?
Give these a try, they'll output NSString. Keep in mind, the second set requires importing the Objective-C runtime header.
#import <objc/runtime.h>
NSString *string = #"I'm a string.";
NSLog(#"%#",NSStringFromClass([string classForCoder]));
NSLog(#"%#",NSStringFromClass([string classForKeyedArchiver]));
NSLog(#"%s",class_getName([string classForCoder]));
NSLog(#"%s",class_getName([string classForKeyedArchiver]));
Now, this won't work in all cases. For example, trying to get the class of NSConstantString, in this manner will output NSString. If you require checking the class name as a string in this way, you probably should reconsider your approach to solving the problem.
NSString is a so-called "class cluster". That means that the init methods will return
an instance of some subclass (such as __NSCFConstantString or __NSCFString).
You will never get an instance with the class equal to NSString.
If your intention is to check whether an object is a NSString or not then
use isKindOfClass:
if ([s isKindOfClass:[NSString class]]) {
// this is a string …
}
Other examples of class clusters are NSNumber, NSDictionary, NSArray
and their mutable variants.
NSLog(#"%#", NSStringFromClass([s class]));
This code
if ( [currentValue isKindOfClass:NSClassFromString(#"NSString")] ) {
CBDebug(#"this is a string");
works well when the type of currentValue is __NSCFString
but it doesn't recognize the string if the type is __NSCFConstantString
How can I fix it ?
(I'm on OSX)
thanks
Try this:
if ( [currentValue isKindOfClass:[NSString class]] ) {
CBDebug(#"this is a string");
}
Instead of checking the textstring of the class, do try the following:
[myObject isKindOfClass:[NSString class]]
Try this [obj isKindOfClass:[NSString class]];
Normally you only need NSClassFromString when testing for class names that may not be available to you.
Note: Be careful when using isKindOfClass: 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. isKindOfClass: will return YES if the receiver somewhere inherits from the class passed as argument.
I have a problem with classes. This is my code:
self.shapeClass = [HWRectangle class];
if ([_shapeClass isKindOfClass:[HWRectangle class]]) {
NSLog(#"Class created as: %s", [_shapeClass description]);
}
I thought that the program will do the logging in this case, but it doesn't. Do you have any idea why not?
because: if ([_shapeClass isKindOfClass:[HWRectangle class]])
_shapeClass should be an instance of the class you are testing, unless you are really testing for class comparisons. So, this method is instance to class comparison, not class to class comparison.
For bonus points, your format string should be: NSLog(#"Class created as: %#", [_shapeClass description])
(unless you have overridden the NSObject method (which you should not do))
isKindOfClass checks the class of a variable. You say that shapeCalls = [HWRectangle class]. The result of [HWRectangle class] is of the class "Class". So if you compare this with the class of HWRectangle you will find that the two are not the same.