▼ |  | Objective-C FFI

1 | Using Unsafe Bindings
2 | FFI Types and Constants
3 | Syntactic Forms
4 | Raw Runtime Functions

On this page:

tell
tellv
import-class
define-objc-class
self
super-tell
get-ivar
set-ivar!
selector

top ← prev up next →

### 3`` Syntactic Forms

( 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>

> ( tell ( tell NSString alloc)
initWithUTF8String: #:type _string "Hello")

#<cpointer:id>

( tellv obj-expr method-id)
( tellv obj-expr arg ...)

Like tell, but with a result type _void.

( import-class class-id ...)

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.

Example:
> ( import-class NSString)

( define-objc-class class-id superclass-expr
[field-id ...]
method)

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.

Each field-id is an instance field that holds a Scheme 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.

Example:

> ( define-objc-class MyView NSView
[bm] ; <- one field
( - _scheme (swapBitwmap: [ _scheme new-bm])
( begin0 bm ( set! bm new-bm)))
( - _void (drawRect: [_NSRect exposed-rect])
( super-tell drawRect: exposed-rect)
(draw-bitmap-region bm exposed-rect))
( - _void (dealloc)
( when bm (done-with-bm bm))))

self

When used within the body of a define-objc-class method, refers to the object whose method was called. This form cannot be used outside of a define-objc-class method.

( super-tell result-type method-id)
( super-tell result-type arg ...)

When used within the body of a define-objc-class method, calls a superclass method. The result-type and arg sub-forms have the same syntax as in tell. This form cannot be used outside of a define-objc-class method.

( get-ivar obj-expr field-id)

Extracts the Scheme value of a field in a class created with define-objc-class.

( set-ivar! obj-expr field-id value-expr)

Sets the Scheme value of a field in a class created with define-objc-class.

( selector method-id)

Returns a selector (of FFI type _SEL) for the string form of method-id.

Example:
> ( tellv button setAction: #:type _SEL ( selector terminate:))

top ← prev up next →