[Openmcl-devel] Cocoa Strings in CCL-1.5

Paul Krueger plkrueger at comcast.net
Sun Jul 4 15:37:43 UTC 2010


On Jun 20, 2010, at 4:52 PM, John DeSoi wrote:

> 
> On Jun 17, 2010, at 9:30 AM, Raffael Cavallaro wrote:
> 
>>> 
>>> Does anyone know what I need to do to produce an ns-string object with an arbitrary initial value in the newer versions of CCL?
>> 
>> ccl::%make-nsstring
> 
> Note that you need to arrange to release (or autorelease) any strings you create with ccl::%make-nsstring. This is one of the aspects I miss most about the LispWorks Objective-C bridge -- you can just pass a Lisp string. 
> 
> 
> 
> John DeSoi, Ph.D.
> 
> 

I was out of the country for a bit, but thought it might still be relevant to reply to this.

In my contrib directory in the file ...contrib/krueger/InterfaceProjects/Utilities/ns-string-utils.lisp I define the following function:

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

It's basically a copy of ccl::%make-nsstring except that it uses a class method to create the string rather than an instance method. What's the difference?

The class method is one of the so-called convenience functions that are provided by some cocoa classes. According to Apple's documentation:
	http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.pdf
"Many classes provide methods of the form +className... that you can use to obtain a new instance of
the class. Often referred to as “convenience constructors”, these methods create a new instance of the class,
initialize it, and return it for you to use. You do not own objects returned from convenience constructors, or
from other accessor methods."

Since you don't own them, it is unnecessary to release them later. I've found this to be a fairly convenient tactic for several types of temporary objects that are created in Lisp only to be passed to some Objective-C function (which will retain it if needed). Of course, it isn't guaranteed to remain valid for an arbitrary amount of time either (only for the duration of the current event loop), so if it is needed for some future event processing, this isn't the technique to use.


More information about the Openmcl-devel mailing list