The Node.js ⇆ Objective-C bridge
Перейти к файлу
Rosen Vladimirov 3765c0365a Merge pull request #12 from telerik/vladimirov/node76
Add support for Node.js 7.6.0
2017-02-22 16:46:24 +02:00
docs docs: specify a utf8 doctype for in the template 2012-04-25 19:18:11 -07:00
examples Updated NodeCocoaHelloWorld.app example with NodeJS friendly EventLoop implementation 2015-01-13 22:23:20 -08:00
lib Merge remote-tracking branch 'upstream/master' into vladimirov/support-node-4 2015-10-06 11:04:03 +03:00
test package: update "libxmljs" and use "memwatch-next" 2015-09-29 14:33:57 -07:00
.gitignore memtest.js is faulting on travis-ci, need to look into this closer 2014-08-11 00:19:46 -06:00
.jshintrc .jshintrc: remove "asi" and "laxcomma" 2014-08-07 14:58:29 -07:00
.travis.yml travis: osx on one line 2015-09-29 14:35:12 -07:00
AUTHORS Added authors/contributors doc 2014-07-28 01:22:11 -06:00
History.md Release 2.0.0 2015-04-09 16:22:13 -07:00
LICENSE LICENSE: update year to 2015 2015-01-14 13:48:58 -08:00
Makefile Makefile: fix DOX invocation 2014-08-19 13:06:33 -07:00
README.md rename npm package to lowercase "nodobjc" 2015-04-09 16:21:19 -07:00
global.js Fixed global functionality 2014-07-31 23:32:44 -06:00
package.json Add support for Node.js 7.6.0 2017-02-22 16:45:01 +02:00

README.md

NodObjC

The Node.jsObjective-C bridge

Build Status

NodObjC exposes the Objective-C runtime to Node.js in a high-level, easy to use fashion. It uses the BridgeSupport files to dynamically generate an API from an Objective-C "Framework", and uses node-ffi to dynamically interact with the Objective-C runtime.

Essentially, NodObjC is similar in nature to the other popular Objective-C scripting bridges:

So you can write entire Cocoa or iOS GUI applications entirely in Node.js JavaScript! Applications are interpreted at runtime through the V8 engine, rather than compiled to a machine exectuable. This has the advantage of being able to tweak code without having to recompile; excellent for rapid prototyping and development, or for those GUI applications where absolute speed is not a requirement (i.e. most). So what are you waiting for? Get to coding!

Installation

Install using npm, of course!

$ npm install nodobjc

Or add it to the "dependencies" section of your package.json file.

Hello World

var $ = require('nodobjc')

// First you import the "Foundation" framework
$.framework('Foundation')

// Setup the recommended NSAutoreleasePool instance
var pool = $.NSAutoreleasePool('alloc')('init')

// NSStrings and JavaScript Strings are distinct objects, you must create an
// NSString from a JS String when an Objective-C class method requires one.
var string = $.NSString('stringWithUTF8String', 'Hello Objective-C World!')

// Print out the contents (toString() ends up calling [string description])
console.log(string)
//   → Prints "Hello Objective-C World!"

pool('drain')

Be sure to check out the full API docs.

Introduction

This module offers a bi-directional bridge between Node.js and the Objective-C runtime. What does that mean exactly? Well due to the design of the Objective-C runtime, it is possible to port the entire API to other languages. There are quite a few bridges for Obj-C so one for node was a necessity.

So with this module, you get access to all of the Objective-C APIs, but you invoke them through JavaScript. Obj-C has a concept of "message passing" to invoke methods on objects. The way that you pass messages around is probably a little bit different than the kind of JavaScript you're used to:

// In JavaScript, you invoke a function on an object like:
obj.func(arg)

Compared to:

// In NodObjC, you send a message to an object like:
obj('func', arg)

In Objective-C, the names of methods are part of the arguments that you pass along:

[array insertObject: obj
       atIndex: 5]

The equivalent of the above message invocation in NodObjC syntax would be:

array('insertObject', obj,
      'atIndex', 5)

So the even numbered arguments are the parts of the method name that will be invoked, and the odd numbered arguments are the Obj-C arguments themselves. In the above example, the insertObject:atIndex: function will be invoked.

In NodObjC, not only is the Objective-C runtime exposed, but so are the corresponding C functions that usually go along with these APIs (thanks to BridgeSupport). So for example, we can make an NSRect by calling the NSMakeRect() C function:

$.NSMakeRect(5, 10, 8, 30)
// -> NSRect struct

There's a plethora of other Objective-C resources and tutorials out there.

You should definitely have Apple's official Mac or iOS API docs handy at all times.

Support / Getting Involved

If you're looking for support for developing with/for NodObjC you might want to join the mailing list, and check out the #nodobjc channel in the Freenode IRC server.

Additional topics of discussion can be found on the Wiki page.