Skip to content

Support for method swizzling #31

@darind

Description

@darind

It should be possible to replace the implementation of existing Objective-C classes with method swizzling.

Here's an example of swizzling class methods:

var nativeStaticMethod = TNSSwizzleKlass.staticMethod;
TNSSwizzleKlass.staticMethod = function (x) {
    return 2 * nativeStaticMethod.apply(this, arguments);
};

And the native class method should be replaced by the javascript implementation.

Another example with instance methods:

var nativeInstanceMethod = TNSSwizzleKlass.prototype.instanceMethod;
TNSSwizzleKlass.prototype.instanceMethod = function (x) {
    return 2 * nativeInstanceMethod.apply(this, arguments);
};

And with instance properties:

var nativeProperty = Object.getOwnPropertyDescriptor(TNSSwizzleKlass.prototype, "aProperty");
Object.defineProperty(TNSSwizzleKlass.prototype, 'aProperty', {
    get: function () {
        return 2 * nativeProperty.get.call(this);
    },
    set: function (x) {
        nativeProperty.set.call(this, 2 * x);
    }
});

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions