[racket] FFI: Create reference to Objective C object

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Jan 2 08:56:53 EST 2015

In this example, `kTISPropertyUnicodeKeyLayoutData` is already a
`CFStringRef` (a.k.a. `NSString*`).

The type `_NSString` from `mred/private/wx/cocoa/types` (or, better for
most purposes, from `ffi/unsafe/nsstring`) is really a pointer to an
`_NSString`; it just gets tedious to put `Ref` or `-pointer`
everywhere. I'll fix the docs for `_NSString`.

At Fri, 2 Jan 2015 14:37:20 +0100, Jens Axel Søgaard wrote:
> 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 '???)
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.