[Openmcl-devel] What's the minimal way to start using the ObjectiveC bridge?

Leo Liu sdl.web at gmail.com
Mon Apr 28 23:36:44 UTC 2014


On 2014-04-28 12:31 -0500, Paul Krueger wrote:
> Did you mean "setObject:forKey:" ?  There is no standard addObject:forKey: objective-C method. You also need to understand the correct syntax for calling obj-c methods in CCL:
>
> ? (defun lisp-to-temp-nsstring (string)
>   ;; creates a string that is not owned by caller
>   ;; so no release is necessary
>   (with-encoded-cstrs :utf-8 ((s string))
>     (#/stringWithUTF8String: ns:ns-string s)))
> LISP-TO-TEMP-NSSTRING
> ? (setf key (lisp-to-temp-nsstring "key string"))
> #<NS-MUTABLE-STRING "key string" (#x117690)>
> ? (setf obj (make-instance ns:ns-object))
> #<NS-OBJECT [uninitialized] (#x21074380)>
> ? (setf dict (make-instance ns:ns-mutable-dictionary))
> #<NS-MUTABLE-DICTIONARY {
> } (#x121D60)>
> ? (#/setObject:forKey: dict obj key)
> NIL
> ? (describe dict)
> #<NS-MUTABLE-DICTIONARY {
>     "key string" = "<NSObject: 0x21074380>";
> } (#x121D60)>
> Class: #<OBJC:OBJC-CLASS NS:NS-MUTABLE-DICTIONARY (#x7FFF7AF09618)>
> Wrapper: #<CCL::CLASS-WRAPPER NS:NS-MUTABLE-DICTIONARY #x302000C665CD>
> Instance slots
> NS:ISA: #<NS-OBJECT __NSDictionaryM (#x7FFF7AF09578)>
> ? (#/objectForKey: dict key)
> #<NS-OBJECT [uninitialized] (#x21074380)>
> ? (eql obj *)
> T
> ? 
>
> You could use standard #/allocate and #/init calls instead of make-instance calls. This just demonstrates how obj-c has been integrated into CLOS.

Many thanks for this example. With that I think I can follow along with
your objc-bridge talk in ECLM 2013.

I forgot where I copied the example from. But was very surprised any
mistake can crash lisp.

Thanks,
Leo



More information about the Openmcl-devel mailing list