I have following piece of code
NSMutableArray *mutArray = [[NSMutableArray alloc] init];
[mutArray addObject: [NSProcessInfo processInfo]];
[mutArray addObject: @"This is NSString Object"];
[mutArray addObject: [[NSMutableString alloc] initWithString: @"1st Mutable String"]];
for (id element in mutArray){
NSLog(@" ");
NSLog(@"Class Name: %@", [element className]);
NSLog(@"Is Member of NSString: %@", ([element class] isMemberOfClass: [NSString class]) ? YES: NO);
NSLog(@"Is kind of NSString: %@", ([element class] isKindOfClass: [NSString class]) ? YES: NO);
}
I am getting following output (and expecting as pointed)
Class Name: NSProcessInfo
Is Member of NSString: NO
Is Kind of NSString: NO
Class Name: NSCFString <-- Expecting NSString
Is Member of NSString: NO <-- Expecting YES
Is Kind of NSString: NO <-- Expecting YES
Class Name: NSCFString <-- Expecting NSMutableString
Is Member of NSString: NO
Is Kind of NSString: NO <-- Expecting YES
Am I missing something terrible simple here? Thanks!
Use:
[element isMemberOfClass: [NSString class]]
Not:
[[element class] isMemberOfClass: [NSString class]]
NSString and NSMutableString are implemented as a class cluster (see "String Objects" in the iOS version of the documentation).
So isKindOfClass: should return true but isMemberOfClass: will return false since NSString isn't the exact type of the object.
NSString
is made up of a cluster of classes. They are also toll-free-bridged with CFString
s (from CoreFoundation
). It's very likely somewhere in the implementation of NSString
this NSCFString
appears (I don't know all the facts, but my deduction here is this class acts as the bridge).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With