Skip to content

Instantly share code, notes, and snippets.

@darknoon
Created March 24, 2017 21:46
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/ce9d5dfa78889ea9585c4f0d15e97087 to your computer and use it in GitHub Desktop.
Save darknoon/ce9d5dfa78889ea9585c4f0d15e97087 to your computer and use it in GitHub Desktop.
How to use ObjCClass library
const MyClass = new ObjCClass({
classname: 'MyClass',
superclass: NSButton,
_private: 'initial',
init() {
log("my custom init code. no need to call super here ");
this._private = 'test';
},
test() {
log("test: " + this._private);
},
description() {
const ret = `<MyClass _private=${this._private}>`;
return NSString.stringWithString(ret);
}
})
const obj = MyClass.new();
log(`obj ${obj}`);
obj.test();
// But this is more elegant!
obj._private = "efgh";
obj.test();
@darknoon
Copy link
Author

darknoon commented Mar 24, 2017

If you do want to call super and use the result:

description() {
  // Cache appropriate function, don't look it up each time
  if (typeof MyClass._superDesc == 'undefined') {
    MyClass._superDesc = SuperCall(NSStringFromSelector('description'), [], {type:"@"});
  }
  const superDesc = MyClass._superDesc.call(this);
  return NSString.stringWithString(`${superDesc} { _private=${this._private} }`);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment