📺 "One project. One language. Three apps." Learn more in our upcoming code sharing 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;
// ...
@end

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.