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 if they were global variables.)
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 |
Since Joy 2.1, Objective-C classes can define the semantics of JavaScript properties for their instances by implementing the ITKJSProperties protocol. For example, in an application built on the Enterprise Objects Framework, you could easily implement it for your business objects, so you can access EOF properties using the convenient syntax object.property instead of accessor methods. Joy provides implementations of this protocol for the NSArray and NSDictionary classes:
Elements of NSArray objects can be accessed like JavaScript array elements, so you NSDictionary objects can be used like associative arrays. You can write dictionary[key] instead of [dictionary objectForKey: key], and, for mutable dictionaries, dictionary[key] = value instead of [dictionary setObject: value forKey: key] and delete dictionary[key] instead of [dictionary removeObjectForKey: key]. Enumerating an NSDictionary with for...in will loop over its keys. If the ITKJSProperties protocol is not implemented for a class, then its instance variables will be reflected as JavaScript properties of the corresponding Id object. You can also assign values to any other JavaScript property, and they will be remembered, so each object can have its own set of additional, "run-time" instance variables. Enumerating such an Id object with for...in will loop over its Objective-C instance variables and all properties that were explicitly assigned from JavaScript. 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, because *array will be treated the same as array[0] and NSArray's implementation of ITKJSProperties defines this as [array objectAtIndex: 0]). You can get the same struct by using the non-enumerated property id["#ivars"]. This works for all objects, even NSArrays. |
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 property). 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.
The Id object itself provides two JavaScript methods, toString() and toSource() for conversion to a JavaScript string. The toString() method is intended for human consumption, while the toSource() method produces a string like (Class *)0xaddress which can be evaluated as an expression to get the same Id object back. |
SEE ALSO |
Objective-C Message Expression @teach ObjC.unteach |
Index |