Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing NSCFString and NSCFBoolean

I am using SBJsonParser to parse JSON. An input can be 0 or a string (ex. a829d901093), and if it's zero, NSCFBoolean is returned, if its string NSCFString is returned. How can I tell which one is returned? Thanks!

like image 636
user635064 Avatar asked Feb 24 '23 20:02

user635064


1 Answers

Calling these by their internal toll-free names is what makes this confusing. If you call them NSNumber and NSString (as they are listed in the documentation), then the answer is clear:

if ([value isKindOfClass:[NSNumber class]]) { ... }

EDIT: @Magnus points out that it isn't obvious that a NSCFBoolean isa NSNumber in order to look them up. That's true. To me it's very obvious because I know the Core Foundation type system and I know what the toll free bridge classes mean and how they're implemented (it's one of the coolest tricks in all of Cocoa IMO). But what if you didn't know those things? It's still no problem.

  • In the debugger, look in the variable list and expand the variable you care about. Its first "member" will be it superclass. Expand. Continue until you find a class you know.

  • Alternately, you can walk the superclasses using NSStringFromClass([object superclass]). Keep tacking superclass on for as many levels as you'd like to check.

like image 182
Rob Napier Avatar answered Mar 02 '23 23:03

Rob Napier