Description

A basic interface to Objective-C

Author

felix

Version

Requires

Usage

(require-extension objc)

Download

objc.egg

Documentation

This extension allows to interact with Objective-C code with a minumum of fuss (sort of).

Syntax

macro: (define-objc-classes NAME ...)

Locates the ObjC classes NAME ... and defines variables holding pointers to the class objects. NAME may be a symbol or a list of the form (VARIABLE CLASSNAME). For example, (define-objc-classes NSTextView NSTask) is equivalent to

(begin
  (define NSTextView (objc:string->class "NSTextView"))
  (define NSTask (objc:string->class "NSTask")))

Read syntax

Read syntax: @"..."

Creates an instance of NSString with the given characters as its contents. Currently assumes UTF8 encoding.

Read syntax: @[RECEIVER KEYWORD ARGUMENT ...]

An abbreviation for (objc:send RECEIVER KEYWORD ARGUMENT ...). If the receiver is prefixed with the safe: keyword, then this form expands into a (objc:send/safe ...) expression.

Method invocation

macro: (objc:send RECEIVER KEYWORD1 ARGUMENT1 ...)

Invokes the method named by KEYWORD1 ... for the given receiver, which may be either an Objc instance or a class. The keywords should follow the normal ObjC syntax.

macro: (objc:send/safe RECEIVER KEYWORD1 ARGUMENT1 ...)

Equivalent to (objc:send ...) but allows callbacks into Scheme from Objective-C code.

Helper procedures

procedure: (objc:instance? X)

Returns #t if X is an object representing an ObjC instance or #f otherwise.

procedure: (objc:class? X)

Returns #t if X is an object representing an ObjC class or #f otherwise.

procedure: (objc:selector? X)

Returns #t if X is an object representing an ObjC selector or #f otherwise.

procedure: (objc:instance->pointer INSTANCE)

Returns the raw pointer for an ObjC instance object.

procedure: (objc:pointer->instance POINTER)

Converts a raw pointer to an ObjC instance into an instance-object.

procedure: (objc:pointer->class POINTER)

Converts a raw pointer to an ObjC clas into a class-object.

procedure: (objc:nsstring STRING)

Allocates an instance of NSString with the contents of STRING. Currently assumes UTF8 encoding.

procedure: (objc:string->class STRING)

Returns a class object representing the ObjC class with the name STRING.

procedure: (objc:string->selector STRING)

Returns a selector object representing the message selector with the name STRING.

procedure: (objc:selector->signature INSTANCE SELECTOR)

Returns an instance of NSMethodSignature for the given instance and selector.

Convenience procedures

procedure: (objc:nslog STRING)

Cocoa helpers

The cocoa extension provides some basic stuff for using Cocoa. To use these definitions, execute (use cocoa).

procedure: (objc:nsbeep)

Beeps.

procedure: (objc:nsapplication-main)

Invokes NSApplicationMain() with the appropriate arguments for argc and argv.

Examples

(use objc)

(define-objc-classes NSObject NSString)

(print "NNSObject: " NSObject)

(define sel (objc:string->selector "cancelPreviousPerformRequestsWithTarget:selector:object:"))
(print "hairy selector: " sel)
(define sig (objc:selector->signature NSObject sel))
(print "hairy signature: " sig)
(print "nice signature: " (objc:method-signature NSObject sel))

(define x (objc:send (objc:send/safe NSObject alloc) init))
(pp x)
@[x release]

(define s1 @[NSString stringWithCString: "Oink!"])
(pp s1)

(define s0 @"this is a test")
(pp s0)
(pp @[s0 length])

License

Copyright (c) 2005, Felix Winkelmann.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the Software),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.