Skip to content

Instantly share code, notes, and snippets.

@oleavr
Last active August 29, 2015 14:22
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 oleavr/dfd568c2fb3315f3a7fc to your computer and use it in GitHub Desktop.
Save oleavr/dfd568c2fb3315f3a7fc to your computer and use it in GitHub Desktop.
ObjC class creation API draft 3
const NSObject = ObjC.classes.NSObject;
const NSURLConnectionDataDelegate = ObjC.protocols.NSURLConnectionDataDelegate;
const MyConnectionDelegateProxy = ObjC.registerClass({
name: 'MyConnectionDelegateProxy',
super: NSObject,
protocols: [NSURLConnectionDataDelegate],
methods: {
'- init': function () {
console.log('- init');
const self = this.super.init();
if (self !== null) {
ObjC.bind(self, {
foo: 1234
});
}
return self;
},
'- dealloc': function () {
console.log('- dealloc');
ObjC.unbind(this.self);
this.super.dealloc();
},
'- connection:didReceiveResponse:': function (connection, response) {
console.log('- connection:didReceiveResponse:');
console.log(connection);
console.log(response);
/* this.data.foo === 1234 */
},
/*
* But those previous methods are declared assuming that either the
* super-class or a protocol we conform to has the same method so we
* can grab its type information. However if that's not the case,
* you would write it like this:
*/
'- connection:didReceiveResponse:': {
retType: 'void',
argTypes: ['object', 'object'],
implementation: function (connection, response) {
}
},
/* Or even more verbosely (not recommended as it's hard to do this portably): */
'- connection:didReceiveResponse:': {
types: 'v32@0:8@16@24',
implementation: function (connection, response) {
}
}
}
});
const proxy = MyConnectionDelegateProxy.alloc().init();
/* use `proxy`, and later: */
proxy.release();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment