ios - 在变量上使用 POP 时,类没有初始值设定项

标签 ios swift initialization protocols viewcontroller

我想在我的一个应用程序中应用面向协议(protocol)的编程。我创建了一个名为“CustomAnchor”的协议(protocol)来摆脱自动布局所需的大量命令。但是当我将此协议(protocol)分配给常量(例如 profileImage)时, Controller 会出现以下错误:

Class 'ProfileController' has no initializers

在常数上我收到这个灰色警告:

  1. Stored property 'profileImageView' without initial value prevents synthesized initializers

代码如下:(在底部使用 POP)

class ProfileController: UIViewController {

 override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.white
    self.navigationItem.title = "Profil"
    self.navigationItem.largeTitleDisplayMode = .never
    fillData()
    setupView()
    confBounds()
 }

 func fillData() {
    profileImageView.image = UIImage(named: "test")
 }

 func setupView() {
    view.addSubview(profileImageView)
 }

 func confBounds() {
    profileImageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    profileImageView.anchor(top: self.view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 60, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 140, height: 140)
 }

 let profileImageView: UIImageView, CustomAnchor = { () -> UIImageView in
    let pView = UIImageView()
    pView.contentMode = .scaleAspectFill
    pView.clipsToBounds = true
    pView.image = UIImage(named: "test")
    return pView
 }()
}

最佳答案

因为没有人回答,所以我做了一个简单的解决方法,并将属性交给了 Controller 。不是我真正想要的(平滑的 POP),但它有效..

关于ios - 在变量上使用 POP 时,类没有初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160173/

相关文章:

c++ - 新的对象变体

javascript - Backbone 在初始化时看不到模型更改。正常吗?

内部有资源的 IOS 静态框架

ios - 为什么prepare for segue不能快速发送图像?

ios - 你如何在 Swift 中预加载横幅 iAds?

swift - 将阴影转换到 ARKit 中的 SCNNode

ios - 如何重新创建 xcdatamodeld

ios - 在解析中访问日期列 - Swift IoS 开发

ios - 如何在 Xcode 中修复 "Type of expression is ambiguous without more context"

Java:HashMap 内联初始化的性能影响