[Impromptu] Formatting strings (printf-style)

Andrew Sorensen andrew at moso.com.au
Thu Mar 12 00:13:11 GMT 2009


Yep, no variadic (snazzy name for variable length) methods allowed.   
This generally isn't a problem as there are very few of them and they  
usually have alternatives.  Unfortunately not stringWithFormat ;)  You  
could get around this by writing your own objc method that accepted an  
nsarray (containing your arguments) and then called the  
stringWithFormat method.

Of course in your example you could just as easily write a simple  
utility function:

(define set-precision
    (lambda (real precision)
       (let ((prec (expt 10 precision)))
          (/ (real->integer (* real prec)) prec))))


(print (set-precision 3.141592 2))

Cheers,
Andrew.


On 12/03/2009, at 2:18 AM, Glen F wrote:

> Hi there -- a quick question: how to format strings?  I've tried
> using NSString, but I'm not super-familiar with the ObjC bridge...
> It doesn't seem to want to work with "stringWithFormat:", which takes
> a variable number of arguments.  Impromptu returns "too many
> arguments passed, ignoring additional arguments".
>
> What am I doing wrong?
>
> ;----------
> (define nsstring (objc:call "NSString" "stringWithFormat:" "rounded:
> %.2f" 3.14159))
> (print nsstring)
> ;----------
>
> This prints out (besides the logged warning about 'too many  
> arguments'):
>
> #<OBJC: rounded: 0.00>
>
> Thanks,
> Glen.
>
> _______________________________________________
> Impromptu mailing list
> Impromptu at lists.moso.com.au
> http://lists.moso.com.au/mailman/listinfo/impromptu



More information about the Impromptu mailing list