🤖 Create a chatbot for your NativeScript app! Learn more at our upcoming webinar.

NativeScript Core

Objective-C Categories

Objective-C categories are a powerful mechanism for extending existing Objective-C classes or grouping common APIs together.

Consider the NSURLPathUtilities category on NSURL:

@interface NSURL (NSURLPathUtilities)
+ (NSURL *)fileURLWithPathComponents:(NSArray *)components;
@property (readonly, copy) NSArray *pathComponents;
// ...

It adds on the Objective-C NSURL class some properties and methods.

They will be exposed as static methods on the JavaScript constructor function or instance methods and properties on the prototype object generated for NSURL (see Objective-C Classes).

NOTE: This applies also to Objective-C categories added by third-party frameworks.

You can use them from JavaScript:

var url = NSURL.fileURLWithPathComponents(["foo", "bar"]);
console.log(url); // "foo/bar -- file:///"
console.log(url.pathComponents); // ["/", "foo", "bar"]

Objective-C categories can implement Objective-C protocols and their methods and properties will be also exposed.