Right Approach to interact Objective-C Classes in CocoaScript

Right Approach to interact Objective-C Classes in CocoaScript-Singsys

In this post we will learn the basics of how to use CocoaSpirit for interacting with Objective-C. Let us consider the following example.


Example of Objective-C class

@interface Object1 : NSObject
@property id value1;
@property NSArray *array1;
- (void)selector1;
- (void)selector2:(id)arg1 withArg2:(id)arg2;


Basic Syntax
There are two approach of interacting with objective-C class

//Style One Mocha Javascript style
var object = Object1.alloc().init()
var value1 = object.value1()
var item1 = object.array().first()
object.selector2_withArg2_(arg1, arg2)

// Style Two CocoaScript Objective-C Bracket
var object = [[Object1 alloc] init]
var value1 = [object value1]
var item1 = [[object array] first] [object selector1]
[object selector2:arg1 withArg2:arg2]

It is to be noted that “bracket syntaxes” will work for Cocoa objects but not with Javascript objects.



// Objective-C
NSMutableArray *array = [NSMutableArray array]
[array addObject:@1];
[array addObject:@2];

Array access is very straight forward although you won’t be able to implement the for…in loop in Objective-C arrays:

for (var i = 0; i < array.length; i++) {
var item = array[i]



// Objective-C

NSDictionary *dict = @{ @”key1″: @”value1″, @”key2″: @”value2″ };

It is as easy as in case of Objective-C



Target Action
There’ll be cases where you are required to implement action handler for NSButton. Now we will use the class COSTarget of CocoaScript framework.

var okButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]
var userClickedOK = false
[okButton setCOSJSTargetFunction:function(sender) {
userClickedOK = true


An easy example of completion handler to implement:

typedef void(^CompletionHandler)(BOOL);
@interface Object2 : NSObject
- (void)completionHandler:(CompletionHandler)handler;

Use the following code to implement it

var object2 = Object2.alloc().init() object2.completionHandler(function(success) {
 log(“success: ” + success)



Using  WebFrameLoadDelegate inside OSX as an example:

@interface NSObject (WebFrameLoadDelegate)
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame;


Using MochaJSDelegate, we will create a dynamic instance for callback handling.

@import ‘MochaJSDelegate.js’
// Make sure the object’s lifecycle is there

// Create a WebView
var webView = WebView.new();

// Create a delegate
var delegate = new MochaJSDelegate()

// Configure the handler
delegate.setHandlerForSelector(“webView:didFinishLoadForFrame:”, function(webView, webFrame){
var app = [NSApplication sharedApplication];
[app displayDialog:"WebView Loaded!" withTitle:"Success!"];

// Set WebView’s frameLoadDelegate


Subclass is somewhat tricky just like implementing the delegate pattern.

// Class.js var
Class = function(className, BaseClass, selectorHandlerDict) {
var uniqueClassName = className + NSUUID.UUID().UUIDString();
var delegateClassDesc = MOClassDescription.allocateDescriptionForClassWithName_superclass_(uniqueClassName, BaseClass);
for (var selectorString in selectorHandlerDict) {
delegateClassDesc.addInstanceMethodWithSelector_function_(selectorString, selectorHandlerDict[selectorString]);
return NSClassFromString(uniqueClassName);

Creation of subclass is done as below:

@import ‘Class.js’
var Dog = Class(“Dog”, NSObject, {
“method1″:function() { log(“method1 called”) },
“method2:andArg2:”:function(arg1, arg2) { log(“method2 called. arg1 = ” + arg1 + ” arg2 = ” + arg2) }
var dog = Dog.alloc().init()
[dog method1]
[dog method2:"bark" andArg2:"wow"]



In case of few methods working with pointers is required

@interface MyClass : NSObject
- (CGFloat)getWidthOfRect:(const NSRect *)rect;

We’ll need to use the MOPointer class in Mocha.

var rect = NSMakeRect(0.0, 0.0, 100.0, 100.0)
var ptr = MOPointer.alloc().initWithValue_(rect); myObject.getWidthOfRect_(ptr); >>> 100.0;


Looking forward to respond to your queries and comments regarding approach to fix WordPress security issues. We’ve  covered all the  essential syntax for CocoaScript

About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile applicationWeb application or an E-commerce solution

You may be interested in following:

  1. Beautiful Video Background View Using UIWebView(Objective-C & Swift 3.0)