Passing NSArray Pointer Rather Than A Pointer To a Specific Type
- by mattmccomb
I've just written a piece of code to display a UIActionSheet within my app. Whilst looking at the code to initialise my UIActionSheet something struck me as a little strange. The initialisation function has the following signature...
initWithTitle:(NSString *)title delegate:(id UIActionSheetDelegate)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles
As you can see the otherButtonTitles parameter is a pointer to a String. In my code I set it as follows...
otherButtonTitles: @"Title", @"Date", nil
Although this compiles fine I don't really understand how it works. My reading of the statement is that I have created an inline array containing two elements (Title and Date). How come this then compiles? I'm passing a NSArray* in place of a NSString*. I know from a little of understanding of C++ that an array is really a pointer to the first element. So is this inline array that I'm creating a C array as opposed to a NSArray?
What I'm hoping to achieve is to be able to pass a static NSArray* used elsewhere in my class to the otherButtonTitles parameter. But passing the NSArray* object directly doesn't work.