NSString array to -> char**
NSString array to -> char**
- Subject: NSString array to -> char**
- From: Seth Delackner <email@hidden>
- Date: Sat, 15 Mar 2003 20:23:57 -0800
Has anyone come up with a better method of converting a set of
NSStrings to a char** that can be passed to library functions expecting
an array of cstrings?
My solution below is nasty, because it trusts the user to free the
memory (when they probably have no idea what the size of the memory is):
@interface NSString (Delackner)
+(char**)toCStrings:(NSString*) arg1, ... ;
@end
@implementation NSString (Delackner)
+(char**)toCStrings:(NSString*) arg1, ... {
char** cargs;
va_list varargs;
NSString* arg;
NSMutableArray* args = [NSMutableArray arrayWithCapacity: 4];
[args addObject: arg1];
va_start(varargs, arg1);
for (arg = va_arg(varargs, NSString*); arg != nil; arg =
va_arg(varargs, NSString*))
{
[args addObject: arg];
}
va_end(varargs);
int i = [args count];
cargs = (char**)malloc(sizeof(char**) * i);
while (--i >= 0) {
arg = (NSString*)[args objectAtIndex: i];
cargs[i] = (char*)malloc(sizeof(char) * [arg length] + 1);
cargs[i] = (char*)[arg cString];
}
[args release];
return cargs;
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.