Created
March 24, 2017 18:11
-
-
Save darknoon/3f6a9ad9d2c1f9495cd5a7824f62c2b4 to your computer and use it in GitHub Desktop.
Create any arbitrary structure when you don't have a constructor exported for it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Recursively create a struct | |
function makeStruct(def) { | |
if (typeof def !== 'object') { | |
return def; | |
} | |
const name = Object.keys(def)[0]; | |
const values = def[name]; | |
const structure = MOStruct.structureWithName_memberNames_runtime(name, Object.keys(values), null); | |
Object.keys(values).map( member => { | |
structure[member] = makeStruct(values[member]); | |
}); | |
return structure; | |
} | |
// Usage, ie if we didn't have the definition of CGRectMake | |
function CGRectMake2(x, y, width, height) { | |
return makeStruct({ | |
CGRect:{ | |
origin:{CGPoint:{x,y}}, | |
size:{CGSize:{width, height}}, | |
}}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment