gpt4 book ai didi

ios - swift 'does not have a member named'

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:26 26 4
gpt4 key购买 nike

这个问题有解决办法吗?

class ViewController : UIViewController {
let collectionFlowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: collectionFlowLayout)
}

xcode 给我以下错误

ViewController.swift: 'ViewController.Type' does not have a member named 'collectionFlowLayout'

我可以使它成为可选的并在 init 方法中初始化它,但我正在寻找一种方法使 collectionview 成为 let 而不是 var

最佳答案

您可以在初始化程序中为常量成员变量分配初始值。无需将其设为 var 或可选。

class ViewController : UIViewController {
let collectionFlowLayout = UICollectionViewFlowLayout()
let collectionView : UICollectionView

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
{
self.collectionView = UICollectionView(frame: CGRectZero,
collectionViewLayout: self.collectionFlowLayout);

super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil);
}

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

关于ios - swift 'does not have a member named',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com