2017-04-12 65 views
0

PhotoCollectionViewDelegate具有初始化器 -问题与传递展开参考目标C初始化

-(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

+0

是的,但不知道如何让MyViewController! (可选值)使用self,因为self给出MyViewController(解包值) – BaSha

回答

1

这可能是愚蠢的,但你尝试过:

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

+0

没有运气,给出 - 无法将类型'(NSObject) - >() - > MyViewController'的值转换为键入'MyViewController!'在强制 – BaSha

+0

事实上,这在稍后的懒惰属性。 – BaSha

1

试试看:

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