OC调用Swift便利构造方法

问题: OC调用Swift构造方法,自定义参数,我想让day变成可选参数,所以加了问号?,导致objc无法识别,报错' Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C'

解决:开始认为是Int类型OC无法识别,换了NSInteger也一样的报错,上网也没有搜到相关问题,然后我换成了普通方法进行调用,发现这个类型可以调用,我去掉问号后发现不报错了,OC可以正常识别传递参数,但是不知道想传递'nullable'的参数改怎么调用。

Swift控制器示例代码

class GWPeriodViewController: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.modalPresentationStyle = .overFullScreen
        self.modalTransitionStyle = .crossDissolve
    }
    
    //@objc public convenience init(days: Int?)无法使用
    @objc public convenience init(days: Int) {
        self.init()
        self.days = days
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

OC调用示例代码

GWPeriodViewController *vc = [[GWPeriodViewController alloc] initWithDays:days];
    [self presentViewController:vc animated:YES completion:nil];

你可能感兴趣的:(OC调用Swift便利构造方法)