简体   繁体   English

为什么我收到错误“无法将类型(类)的值分配给类型 UICollectionViewDelegate、UICollectionViewDataSource?”

[英]Why I get error “cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource?”

When I declare my collection view, I got error "cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource":当我声明我的集合视图时,出现错误“无法将类型(类)的值分配给类型 UICollectionViewDelegate、UICollectionViewDataSource”:

let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    return collectionView
}()

But when I add "lazy var" the error is gone.但是当我添加“lazy var”时,错误就消失了。 I don't know why?我不知道为什么? Can someone explain for me?有人可以为我解释吗?

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    return collectionView
}()

That the closure is called during initialization so you cannot use self to access any properties or methods of the instance yet.闭包在初始化期间被调用,因此您还不能使用 self 访问实例的任何属性或方法。 If you need to access self you must replace the let with lazy var.如果您需要访问 self ,则必须将 let 替换为惰性 var。

let collectionView: UICollectionView = {
   let layout = UICollectionViewFlowLayout()
   let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
   collectionView.delegate = self // You cannot use
   collectionView.dataSource = self // You cannot use
   return collectionView 
}()

adding lazy forces iOS to check for instantiation of collectionView only when it is needed for the first time.添加惰性强制 iOS 仅在第一次需要时检查 collectionView 的实例化。 It therefore not gives you an error at compilation.因此,它不会在编译时给您错误。 Prior to that, it was giving an error because the initialization was yet to be completed and you were setting properties on the same.在此之前,它给出了一个错误,因为初始化尚未完成并且您正在设置相同的属性。

   let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    return collectionView
}()

You can not access self until its initialized.在初始化之前,您无法访问 self 。 As there is no object of your class till now.因为到目前为止您的 class 还没有 object。 Instance method(s) and variable belong to the Object of the class not to the class ie they can be called after creating the Object of the class. Instance method(s) and variable belong to the Object of the class not to the class ie they can be called after creating the Object of the class. So it gives you error.所以它给你错误。

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    return collectionView
}()

lazy var indicate to skip this variable on the time of intialization. lazy var 表示在初始化时跳过该变量。 If any variable marked as lazy it will not allocated until it is used for first time.如果任何标记为惰性的变量在第一次使用之前不会分配。 You have marked this computed variable as lazy.您已将此计算变量标记为惰性。 so, whenever it's going to be used by any of the function in class, it will always get the object of class (self) allocated.所以,只要它被 class 中的任何 function 使用,它总是会得到 object 的 ZA2F212selfED4F8EBC26CBBD1 分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型NSObject-()的名称分配给uicollectionviewdelegate类型 - Cannot assign value of type NSObject -() Classname to type uicollectionviewdelegate 为什么UICollectionViewDelegate和UICollectionViewDataSource是分开的协议? - Why are UICollectionViewDelegate and UICollectionViewDataSource separate protocols? 无法将类型“ Class”的值分配给类型“ [Class]” - Cannot assign value of type 'Class' to type '[Class]' 我试图掉一个针脚,但出现此错误:无法将类型“ CLLocationManager”的值分配给类型“ CLLocationCoordinate2D” - Im trying to drop a pin but I get this error: Cannot assign value of type 'CLLocationManager' to type 'CLLocationCoordinate2D' UICollectionViewDelegate和UICollectionViewDataSource方法未调用 - UICollectionViewDelegate and UICollectionViewDataSource methods not called UIView中的UICollectionViewDataSource,UICollectionViewDelegate - UICollectionViewDataSource, UICollectionViewDelegate in UIView 错误:无法分配类型为“AnyObject?”的值到“NSURL”类型的值 - Error: Cannot assign a value of type 'AnyObject?' to a value of type 'NSURL' Swift:无法为“ int”类型的值分配“ int”类型的值? 错误 - Swift: Cannot assign a value of type 'int' to a value of type 'int?' Error 无法分配类型“ Class”的值 <T> &#39;键入&#39;协议 <UIImagePickerControllerDelegate, UINavigationControllerDelegate> &#39; - Cannot assign value of type 'Class<T>' to type 'protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?' 无法分配值类型 - Cannot not assign Value Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM