5.7 Objective-C FFI
The library supports Objective-C interaction in two layers. The upper
layer provides syntactic forms for sending messages and deriving
subclasses. The lower layer is a think wrapper on the
Objective-C
runtime library functions. Even the upper layer is unsafe and
relatively low-level compared to normal Racket libraries, because
argument and result types must be declared in terms of FFI C types
(see Type Constructors).
5.7.1 FFI Types and Constants
The type of an Objective-C object, an opaque pointer.
The type of an Objective-C class, which is also an
_id.
The type of an Objective-C protocol, which is also an
_id.
The type of an Objective-C selector, an opaque pointer.
The Objective-C boolean type. Racket values are converted for C in the
usual way: #f is false and any other value is true. C values
are converted to Racket booleans.
Synonym for #t
Synonym for #f
5.7.2 Syntactic Forms and Procedures
(tell result-type obj-expr method-id) |
(tell result-type obj-expr arg ...) |
|
result-type | | = | | | | | | | | #:type ctype-expr | | | | | | arg | | = | | method-id expr | | | | | | #:type ctype-expr method-id arg |
|
Sends a message to the Objective-C object produced by
obj-expr. When a type is omitted for either the result or an
argument, the type is assumed to be
_id, otherwise it must
be specified as an FFI C type (see
Type Constructors).
If a single method-id is provided with no arguments, then
method-id must not end with :; otherwise, each
method-id must end with :.
Examples: |
> (tell NSString alloc) |
#<cpointer:id> |
|
#<cpointer:id> |
(tellv obj-expr method-id) |
(tellv obj-expr arg ...) |
Defines each
class-id to the class (a value with FFI type
_Class) that is registered with the string form of
class-id. The registered class is obtained via
objc_lookUpClass.
Defines each
protocol-id to the protocol (a value with FFI type
_Protocol) that is registered with the string form of
protocol-id. The registered class is obtained via
objc_getProtocol.
(define-objc-class class-id superclass-expr | maybe-mixins | maybe-protocols | [field-id ...] | method) |
|
|
maybe-mixins | | = | | | | | | | | #:mixins (mixin-expr ...) | | | | | | maybe-protocols | | = | | | | | | | | #:protocols (protocol-expr ...) | | | | | | method | | = | | (mode result-ctype-expr (method-id) body ...+) | | | | | | (mode result-ctype-expr (arg ...+) body ...+) | | | | | | mode | | = | | + | | | | | | - | | | | | | +a | | | | | | -a | | | | | | arg | | = | | method-id [ctype-expr arg-id] |
|
Defines
class-id as a new, registered Objective-C class (of
FFI type
_Class). The
superclass-expr should produce
an Objective-C class or
#f for the superclass. An optional
#:mixins clause can specify mixins defined with
define-objc-mixin. An optional
#:protocols clause
can specify Objective-C protocols to be implemented by the class.
Each field-id is an instance field that holds a Racket value
and that is initialized to #f when the object is
allocated. The field-ids can be referenced and set!
directly when the method bodys. Outside the object, they can
be referenced and set with get-ivar and set-ivar!.
Each method adds or overrides a method to the class (when
mode is - or -a) to be called on instances,
or it adds a method to the meta-class (when mode is
+ or +a) to be called on the class itself. All
result and argument types must be declared using FFI C types
(see Type Constructors). When mode is +a or -a, the
method is called in atomic mode (see _cprocedure).
If a method is declared with a single method-id and
no arguments, then method-id must not end with
:. Otherwise, each method-id must end with
:.
If the special method dealloc is declared for mode
-, it must not call the superclass method, because a
(super-tell dealloc) is added to the end of the method
automatically. In addition, before (super-tell dealloc),
space for each field-id within the instance is deallocated.
(define-objc-mixin (class-id superclass-id) | maybe-mixins | maybe-protocols | [field-id ...] | method) |
|
Like
define-objc-class, but defines a mixin to be combined
with other method definitions through either
define-objc-class or
define-objc-mixin. The
specified
field-ids are not added by the mixin, but must be a
subset of the
field-ids declared for the class to which the
methods are added.
Returns a selector (of FFI type
_SEL) for the string form of
method-id.
Check whether obj is an instance of the Objective-C class
cls.
5.7.3 Raw Runtime Functions
Finds a registered class by name.
Finds a registered protocol by name.
Interns a selector given its name in string form.
Allocates a new Objective-C class.
Registers an Objective-C class.
Returns the class of an object (or the meta-class of a class).
Adds a method to a class. The
type argument must be a FFI C
type (see
Type Constructors) that matches both
imp and the not
Objective-C type string
type-encoding.
Adds an instance variable to an Objective-C class.
Gets the value of an instance variable whose type is
_pointer.
Sets the value of an instance variable whose type is
_pointer.
The type of an Objective-C instance variable, an opaque pointer.
Calls the Objective-C method on
_id named by
sel.
The
types vector must contain one more than the number of
supplied
args; the first FFI C type in
type is used
as the result type.
Constructor and FFI C type use for super calls.
5.7.4 Legacy Library