[racket] FFI: Create reference to Objective C object

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Jan 2 08:37:20 EST 2015

What is the best way of creating a reference to an existing Objective C object?

Concretely given an CFString how do I get an CFStringRef  (aka pointer
to a CFStringRef).

/Jens Axel


#lang racket
(require ffi/unsafe/objc)
(require ffi/unsafe
         ffi/unsafe/objc
         ffi/unsafe/define
         mred/private/wx/cocoa/types)

;; Import CoreFoundation
(define cf-lib
  (case (system-type)
    [(macosx) (ffi-lib
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
    [else #f]))

(define-ffi-definer define-cf cf-lib #:default-make-fail make-not-available)

;; CFString and NSString are equivalent (?)
(import-class NSString)
(define _CFString _NSString)

;; Create an CFString
(define kTISPropertyUnicodeKeyLayoutData
  (tell (tell NSString alloc)
        initWithUTF8String: #:type _string "kTISPropertyUnicodeKeyLayoutData"))

;; The API needs an CFStringRef that is a pointer to CFString.
(define _CFStringRef (_ptr i _CFString))


(define reference-to-kTISPropertyUnicodeKeyLayoutData '???)

Posted on the users mailing list.