将未包装的引用传递给Objective C初始化程序的问题

时间:2017-04-12 09:56:47

标签: ios objective-c swift

PhotoCollectionViewDelegate有初始化 -

-(instancetype)initWithController:(MyViewController *)controller {
    self = [super init];
    if(self) {
        self.myViewController = controller;
    }
    return self;
}

MyViewController.swift文件中调用

let photoCollectionViewDelegate = PhotoCollectionViewDelegate(controller : self)

gives 'Can not convert value of type MyViewController to expected argument type 'MyViewController!'

PhotoCollectionViewDelegate(controller : MyViewController!),但self与之呼叫MyViewController

2 个答案:

答案 0 :(得分:1)

这可能很愚蠢,但你试过了吗?

let photoCollectionViewDelegate = PhotoCollectionViewDelegate(controller : self as MyViewController!)

答案 1 :(得分:1)

试一试:

let optionalSelf = self as MyViewController!
let wrappedSelf = optionalSelf ?? nil
let photoCollectionViewDelegate = PhotoCollectionViewDelegate(controller : wrappedSelf)