Setup:
I have data coming in via JSON.
Using NSJSONSerialization I convert the JSON to an object.
A number value in JSON comes in Objective-C as several different possible class types:
(NSNumber, NSDecimalNumber or __NSCFNumber
) - all of those are in the class cluster under NSNumber
.
Problem:
1) I need a way to get the class cluster "umbrella" class - NSNumber
, when I have a value of any of the types NSNumber, NSDecimalNumber or __NSCFNumber
.
Same goes for strings. I need a way to get NSString
, when I have any of these: NSString, NSMutableString, __NSCFString, __NSCFConstantString
.
2) It could work also if I could get by code a list of all classes in the NSString cluster for example. Then I can build dynamically at run-time a list, and be sure it is complete.
What I have so far:
So far I couldn't come up with a sane way to do that. So I have a list of allowed types, but I'm afraid it might not be complete + it doesn't feel like the greatest solution there is.
Code: https://github.com/icanzilb/JSONModel/blob/master/JSONModel/JSONModel.m#L45
You can do this:
you can extend the list by adding other classes to the
classes
array. In the end,cls
will contain the class cluster of whichobject
is an instance, orNil
if it isn't an instance of either one.