Search code examples
iosobjective-cswiftinitializervariadic

Calling Objective-C initializer with variadic arguments


I'm trying to re-use an Objective-C class, namely TSAlertView, into a Swift project. The problem is that the class uses an initializer with variadic arguments. I followed the same approach suggested at this stackoverflow question and my code works in the iOS Simulator if I use the iPad Air but NOT if I use the iPad Retina. The code also crashes on a real iPad 3.

I was able to create a toy example that shows the same issue.

TestClass.h

#import <Foundation/Foundation.h>

@interface TestClass : NSObject

@property NSArray *titles;

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args;

@end

TestClass.m

#import "TestClass.h"

@implementation TestClass

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args {

    NSMutableArray *titles = [NSMutableArray array];

    if ((self = [super init])) {
        [titles addObject:title];

        id arg;
        if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { // this causes an EXC_BAD_ACCESS on iPad Retina
            [titles addObject:(NSString*)arg];

            while ( nil != ( arg = va_arg( args, id ) ) ) 
            {
                if ( ![arg isKindOfClass: [NSString class] ] )
                    return nil;

                [titles addObject:(NSString*)arg];
            }
        }
    }
    self.titles = [NSArray arrayWithArray:titles];
    return self;
}

@end

TestClass+Swift.swift

import Foundation

extension TestClass {
    convenience init(title:String?, otherButtonTitles:CVarArgType...)
    {
        self.init(title: title, otherButtonTitlesVA:getVaList(otherButtonTitles))
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let testObject1 = TestClass(title: "First", otherButtonTitles: "Second", "Third")
        let testObject2 = TestClass(title: "First") // this causes the initializer to crash on iPad Retina

        NSLog("%@", testObject1.titles)
        NSLog("%@", testObject2.titles)
    }
}

The code crashes when trying to create testObject2 with an EXC_BAD_ACCESS. Is there anything wrong with my code? Why does the iPad Air show a different behavior from the iPad Retina?

EDIT: OK, I think the problem is that the Objective-C code expects a nil terminated list. How do I pass a nil terminated va_list from Swift?


Solution

  • As you already figured out, the Objective-C code expects a nil terminated list. Without that, the behavior of

    if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { ... }
    

    is undefined if the list of actually passed arguments is exhausted.

    nil is a NULL pointer in Objective-C, and you can append that in your convenience initializer:

    extension TestClass {
        convenience init(title:String?, otherButtonTitles : CVarArgType...)
        {
            let nullPtr = UnsafePointer<Void>()
            let otherTitles : [CVarArgType] = otherButtonTitles + [nullPtr]
            self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
        }
    }
    

    This seems to work correctly on both 32- and 64-bit platforms.


    Update for Swift 3:

    extension TestClass {
        convenience init(title:String?, otherButtonTitles : CVarArg...)
        {
            let otherTitles : [CVarArg] = otherButtonTitles + [Int(0)]
            self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
        }
    }
    

    As mentioned in https://bugs.swift.org/browse/SR-5046, using an Int sized zero is the recommended way to pass a null pointer on a variable argument list.