Ios 无法将NSObject-()类名称类型的值分配给uicollectionviewdelegate类型

Ios 无法将NSObject-()类名称类型的值分配给uicollectionviewdelegate类型,ios,swift,uicollectionviewdelegate,Ios,Swift,Uicollectionviewdelegate,我正在使用view并在其中实现collectionview 当我试图引用datasource并委托给我的collectionView时,它给出了一个错误 我的类文件 class MenuBar : UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ let collectionView : UICollectionView = { let l

我正在使用view并在其中实现collectionview

当我试图引用datasource并委托给我的collectionView时,它给出了一个错误

我的类文件

class MenuBar : UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{

let collectionView : UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31)
    cv.delegate = self  // Error here

    return cv

}()

override init(frame: CGRect){
    super.init(frame: frame)

}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

我无法将委托和数据源提供给我的collectionview 我还实现了以下两种方法

cellForItemAt索引 项目编号部分


任何帮助都将得到感谢,因为您试图在结束时访问self

lazy var collectionView : UICollectionView = {
    [unowned self] in
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31)
    cv.delegate = self  // Error here

    return cv

}()

这应该是可行的,因为您试图在闭包中访问self

lazy var collectionView : UICollectionView = {
    [unowned self] in
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31)
    cv.delegate = self  // Error here

    return cv

}()

这应该可以工作

类菜单栏:UIView
它的视图不是
类菜单栏:UIViewController
类菜单栏:UIView
它的视图不是
类菜单栏:UIViewController
谢谢@Evgeniy Gushchin谢谢@Evgeniy Gushchin