Skip to content

Instantly share code, notes, and snippets.

@darknoon
Created March 24, 2017 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darknoon/3f6a9ad9d2c1f9495cd5a7824f62c2b4 to your computer and use it in GitHub Desktop.
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
// 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