Search code examples
iosobjective-cswiftresearchkit

Swift: using object factory results in unrecognised selector


Am using Josh Smith's ObjectFactory for instantiating classes by name in Swift. But the below code I tried results in unrecognised selector.

if let survey = surveyFactory.createInstance(className: className, initializer: "initWithStyle", argument: textChoiceStyle!.rawValue , argument2:  textChoices!)

The class name I passed is ORKTextChoiceAnswerFormat and the resulting Obj C expression I need to achieve in Swift as below

ORKTextChoiceAnswerFormat *asd = [[ORKTextChoiceAnswerFormat alloc]initWithStyle:<#(ORKChoiceAnswerStyle)#> textChoices:<#(NSArray * __nonnull)#>];

But am getting it as unrecognised selector in the object factory method

    static id OBJCInitWithArg(id  target,
                          SEL initializer,
                          id  argument, id argument2)
{  IMP imp = [target methodForSelector:initializer];
    id (*initFunc)(id, SEL, id, id) = (void *)imp;
   return initFunc(target, initializer, argument, argument2);
}

And am getting this error in the console

[ORKTextChoiceAnswerFormat initWithStyle]: unrecognized selector sent to instance

Where am I going wrong?


Solution

  • The selector for your initializer is initWithStyle:textChoices:.