ios - swift 'does not have a member named'

标签 ios xcode swift var let

这个问题有解决办法吗?

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/

相关文章:

ios - 如何更改用户位置注释的接近度

ios - 在 swift 中存储一个 for in 循环

swift - 如何在 Swift 4 中修复 'unrecognized selector sent to instance'

ios - Auto-Layout Constraint : When resizing, 不平均分配空间给 subview ?

ios - Xcode 模板选择,对成品的影响

arrays - 如何创建具有唯一 "character names"的数组?

ios - 在 gridview 中滚动时字体大小发生变化

ios - IOS 上的 CastHelloText 不会用文本更新屏幕

ios - 在特定的iPhone型号上,StoreKit In App购买似乎崩溃了?

ios - 类型 'Result<Any, AFError>' 的 Alamofire 值没有成员 'isSuccess' (Swift 5)