Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a nil-terminated NSString list as an NSString *

There are many methods in the SDK that ask for a list of strings, terminated by a nil, for example, in UIActionSheet:

- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

'otherButtonTitles' in this case is a list of NSStrings terminated with a nil. What I'd like to do is call this method with a constructed NSMutableArray of NSStrings, because I'd like to create and order the arguments dynamically. How would I do this? I'm not sure how to create a nil-terminated pointer to NSStrings in this case, and if passing it in would even work. Do I have to alloc the memory for it manually and release it?

like image 554
Shaun Budhram Avatar asked Dec 30 '11 21:12

Shaun Budhram


1 Answers

You cannot convert any array into a variadic list.

However, for UIActionSheet, you could add those otherButtonTitles after the sheet is created, using -addButtonWithTitle:

UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:...
                                                        /*etc*/
                                          otherButtonTitles:nil];
for (NSString* otherButtonTitle in otherButtonTitlesArray)
{
   [sheet addButtonWithTitle:otherButtonTitle];
}
like image 80
kennytm Avatar answered Oct 05 '22 07:10

kennytm