Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You would need to use reflection on the passed in class which is not handled as well as some other languages from what i can see.</p> <pre><code>Class c = [self class]; </code></pre> <p>Will return a C struct containing a list of variables and methods and then it would be a matter of writing a data mapper style system to reflect the class and assign the JSON variables to it.</p> <p>E.g. JSON struct of</p> <pre><code>var x = { prop1: "Test 1", prop2: "Test 2}; </code></pre> <p>Then have a class</p> <pre><code>@interface x : NSObject { NSString* prop1; NSString* prop2; } @property (nonatomic,retain) NSString* prop1; @property (nonatomic,retain) NSString* prop2; </code></pre> <p>You'd need to have a concrete implementation but it doesn't need to do anything fancy really.</p> <p>Your mapper could then use either use reflection to parse the object or just use dumb assignment:</p> <pre><code>- (NSObject)mapClassFromJSON:(NSObject)class fromJSON:(id)jsonArray { //code needs to iterate over the jsonArray (needs to check using isKindOfClass if it's an NSArray or NSDictionary) and assign the class properties to it. } </code></pre> <p>Remember if using SBJSON which everyone does, that the types inside the returned JSON can either be NSArray or NSDictionary, it needs to know which. If going down the generic path for nesting then it would need to use variable reflection and some smarts to generate the type mappings as otherwise it needs to be hard coded for each class.</p> <p>Edit: Have a look at </p> <p><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/class_copyPropertyList" rel="nofollow noreferrer">http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/class_copyPropertyList</a></p> <p>Might be useful, might not...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload