Joy Online Manual
NAME |
Id - Represents an Objective-C id |
DESCRIPTION |
Objective-C objects and classes are reflected to JavaScript as objects of the Id type. There is a one-to-one relationship between Id objects and their associated Objective-C objects. Id objects are created automatically by Joy when some Objective-C object needs to be passed to JavaScript, e.g. when you call [class alloc] (Objective-C class objects can be accessed from JavaScript as properties of the global scope).
By default, an Id object has just a weak reference to its Objective-C object (it does not retain it). So it is possible for an Objective-C object to get deallocated while its associated Id object still exists. Attempts to access such a "dead" object will result in a JavaScript error. Use retain and release to avoid references to freed objects or memory leaks, as in Objective-C. You can also create an Id object that does retain its Objective-C object by using the name of an Objective-C class as a JavaScript constructor, e.g. new NSObject. The Objective-C object will be sent a release message automatically when the Id object gets garbage collected (after the last JavaScript reference to it has gone away). This means Joy allows you to have garbage collected Objective-C objects! In addition to the class-as-constructor trick you can make any Objective-C id subject to JavaScript garbage collection by sending it a gcIdJSVal message (the return value is an Id object with a strong reference). This feature frees you from having to worry about retain/release, but at the cost of losing portability to Objective-C. |
PROPERTIES |
All instance variables of Objective-C objects are reflected as JavaScript properties of the corresponding Id object. Enumerating an Id object with for...in will loop over the instance variables.
You can use every Id object as if it were a Pointer object to a Struct containing all its instance variables, so by writing *id you can get an enumeration of an object's contents, and id->ivar is synonymous to id.ivar (this is not true for NSArrays, see next paragraph). You can get at the same instance variable struct by using the hidden property id["#ivars"] - this is safer if you do not know the class of id in advance. Elements of NSArray objects can be accessed like JavaScript array elements, so you |
METHODS |
All methods of Objective-C objects are reflected as methods of the corresponding Id object. The name of each method is the same as the selector name (if a selector name contains colons, you will have to quote it and use array notation to access it as a JavaScript method). You can use both JavaScript and Objective-C syntax to send Objective-C messages to Id objects. You can teach new Objective-C methods to any Id object just by assigning a JavaScript function to the corresponding method slot. You can remove such methods again by using the JavaScript delete operator. |
SEE ALSO |
Objective-C Message Expression @teach ObjC.unteach |
Index |