title: Passing va_list parameter to a variadic function causes bad access error
body: <p>I have a singleton WarningManager class in my project looks as follows</p>
  
  <p>WarningManager.h file</p>
  
  <pre><code>+ (WarningManager *)getInstance;
  
  - (void) createAndPushWarning:(id&lt;UIAlertViewDelegate&gt;)actionDelegate isLocalisedStrings:(BOOL)localized text:(NSString *)text cancel:(NSString *)cancel buttons:(NSString*)firstObj, ...  NS_REQUIRES_NIL_TERMINATION;
  </code></pre>
  
  <p>WarningManager.m file</p>
  
  <pre><code>static WarningManager *instance = nil;
  
  + (WarningManager *) getInstance {
      if (!instance) {
          instance = [[self alloc] init];
      }
      return instance;
  }
  
      - (void) createAndPushWarning:(id&lt;UIAlertViewDelegate&gt;)actionDelegate isLocalisedStrings:(BOOL)localized text:(NSString *)text cancel:(NSString *)cancel buttons:(NSString*)firstObj, ... {
          va_list argumentList;
          va_start(argumentList, firstObj);
  
          NSMutableArray *buttonArray = [NSMutableArray array];
  
          if(firstObj){
              if(!localized){
                  [buttonArray addObject:NSLocalizedString(firstObj, @"")];
              } else {
                  [buttonArray addObject:firstObj];
              }
              id eachObject;
              while ((eachObject = va_arg(argumentList, id))){
                  if(!localized){
                      [buttonArray addObject: NSLocalizedString(eachObject, @"")];
                  }else{
                      [buttonArray addObject: eachObject];
                  }
              }
          }
  
          if (!localized) {
              if(text){
                  text = NSLocalizedString(text, @"");
              }
              if(cancel){
                  cancel = NSLocalizedString(cancel, @"");
              }
          }
  
          va_end(argumentList);
  
          LogW(@"Warning : %@",text);
  
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"My App Name", @"") message:text delegate:actionDelegate cancelButtonTitle:cancel otherButtonTitles:nil];
          for (NSString *button in buttonArray) {
              [alertView addButtonWithTitle:button];
          }
          [alertView show];
      }
  </code></pre>
  
  <p>the above code works well when an objective - C class file calls as follows</p>
  
  <pre><code>[[WarningManager getInstance] createAndPushWarning:self isLocalisedStrings:NO text:@"Text want to display" cancel:nil buttons:@"Button 1", @"Button 2", nil];
  </code></pre>
  
  <p>Now I am creating swift classes and I want to use this same warning manager in it but variadic function syntax changed in swift so I have added another method by replacing variadic arguments as follows in WarningManager class.</p>
  
  <p>WarningManager.h file</p>
  
  <pre><code>- (void) createAndPushWarning:(id&lt;UIAlertViewDelegate&gt;)actionDelegate isLocalisedStrings:(BOOL)localized text:(NSString *)text cancel:(NSString *)cancel agruments:(va_list)buttons
  </code></pre>
  
  <p>WarningManager.M file</p>
  
  <pre><code>- (void) createAndPushWarning:(id&lt;UIAlertViewDelegate&gt;)actionDelegate isLocalisedStrings:(BOOL)localized text:(NSString *)text cancel:(NSString *)cancel agruments:(va_list)buttons {
  
      [self createAndPushWarning:actionDelegate isLocalisedStrings:localized text:text cancel:cancel buttons:(__bridge NSString *)(buttons), nil];
  }
  </code></pre>
  
  <p>To call this from a swift class I have created an extension to WarningManager class as follows in WarningManager+ArgumentList.swift file</p>
  
  <pre><code>extension WarningManager {
  
      class func WarningWrapper(actionDelegate: UIAlertViewDelegate, isLocalizedString: Bool, text:String, cancel: String, _ args: CVarArg...) {
  
          withVaList(args) { _ in WarningManager.getInstance().createAndPushWarning(actionDelegate, isLocalisedStrings: isLocalizedString, text: text, cancel: cancel, agruments: getVaList(args)) }
      }
  }
  </code></pre>
  
  <p>Calling my extension method from swift class</p>
  
  <pre><code> WarningManager.WarningWrapper(actionDelegate: self, isLocalizedString: false, text: "Message to Display", cancel: "OK", "")
  </code></pre>
  
  <p>So when I call this extension from my swift class I am getting bad access error</p>
  
  <p><a href="https://i.stack.imgur.com/gyKkm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gyKkm.png" alt="enter image description here"></a></p>
  
  <p>but I change my WarningManager.m file like this it displays the alert.</p>
  
  <pre><code>- (void) createAndPushWarning:(id&lt;UIAlertViewDelegate&gt;)actionDelegate isLocalisedStrings:(BOOL)localized text:(NSString *)text cancel:(NSString *)cancel agruments:(va_list)buttons {
  
      [self createAndPushWarning:actionDelegate isLocalisedStrings:localized text:text cancel:cancel buttons:nil];
  }
  </code></pre>
  
  <p>Somewhere I am making mistake in sending the arguments I am not sure how to fix this.</p>
  
  <p>any suggestions and ideas are appreciated, Thanks In Advance for people who read this question this far.</p>