📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

NativeScript Core

Objective-C Protocols

Objective-C protocols describe an API Objective-C classes may implement. JavaScript does not have a matching counterpart. For every Objective-C Protocol we expose a JavaScript object that identifies the protocol.

For example given the following Objective-C declarations:

@protocol MyProtocol
- (void)helloWorld;
@end

@interface MyClass : NSObject <MyProtocol>
@end

Will be exposed as:

console.log(MyClass.conformsToProtocol(MyProtocol)); // true

var instance = MyClass.alloc().init();
console.log(instance.conformsToProtocol(MyProtocol)); // true

You can get the name of a protocol as a string and back:

var protocol = NSProtocolFromString("MyProtocol");
console.log(NSStringFromProtocol(protocol)); // "MyProtocol"
console.log(protocol === MyProtocol); // true

Like Objective-C classes, Objective-C protocols too have a prototype and constructor methods. For example:

@protocol TNSProtocol
@property int aProperty;
+ (void)staticMethod;
- (void)instanceMethod;
@end
var staticMethod = TNSProtocol.staticMethod;
var instanceMethod = TNSProtocol.prototype.instanceMethod;
var aProperty = Object.getOwnPropertyDescriptor(TNSProtocol.prototype, 'aProperty');

Although these methods cannot be called directly, it can be useful in some rare cases to get the method from the protocol and call/apply it to an Objective-C object instance that privately conforms to the given protocol.

These protocol wrapper objects can also be used in the extension API to create derived Objective-C classes that implement the protocols.

NOTE: In case of conflicts with other types, the name has the Protocol suffix.

var klass = NSObject;
var protocol = NSObjectProtocol;