Re: Calling variadic Cocoafunction
Re: Calling variadic Cocoafunction
- Subject: Re: Calling variadic Cocoafunction
- From: Sherm Pendley <email@hidden>
- Date: Wed, 7 Sep 2005 20:59:40 -0400
On Sep 7, 2005, at 5:54 PM, Tommy Nordgren wrote:
Is it possible to call a variadic cocoa function from a custom
Objective C++ function.
I'm writing a set of wrapper convenience functions that returns an
Objective C++
smart handle, instead of an autoreleased pointer.
What I want to do with is call [[ NSString alloc] initWithFormat:
@".....", ... ,....],
passing in exactly the same arguments that my own funcion gets
called with.
You could use va_copy() to get a copy of the variadic arguments, then
pass that as the second argument to -initWithFormat:arguments:.
// Typed in email, untested, etc...
void MyClass::func(std::string& fmt, ...) {
NSString *obj_fmt = [NSString stringWithUTF8String:fmt.c_str
()];
va_list src, dest;
va_start(src, fmt);
va_copy(dest, src);
va_end(src);
NSString *result =[[NSString alloc] initWithFormat:obj_fmt
arguments:dest];
}
Alternatively, you could use NSInvocation to call -initWithFormat:.
But then you'd have to parse the format string to determine the
number of arguments to read with va_arg(). I don't know if there's a
function to do that for you - well, there almost certainly is such a
function that's called internally, but I don't know if it's part of
the public API.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden