gpt4 book ai didi

swift - 错误 : Cannot assign to '...' in 'self'

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:56 24 4
gpt4 key购买 nike

我有这个代码

import UIKit

class SliderControllerView: UIView{
private let type: ControlType!
private let label: UILabel!
private let slider: UISlider!
private weak var delegate: SliderControllerDelegate?

private let defaults: SliderDefaults!

convenience init(type: ControlType, defaults: SliderDefaults, delegate: SliderControllerDelegate?) {

self.init()

self.type = type
self.delegate = delegate
self.defaults = defaults

setTranslatesAutoresizingMaskIntoConstraints(false)

label = UILabel()
configureLabel()

slider = UISlider()
configureSlider(slider)

SliderDefaults.set(slider, defaults: defaults)

if let valueFromDetauls = userDefaultsValue {
slider.value = Float(valueFromDetauls)
}

saveValueInUserDefaults()
updateLabel()
}

错误信息:

  1. 无法分配给“self”中的“type”
  2. 无法分配给“self”中的“defaults”
  3. 无法分配给“self”中的“label”
  4. 无法分配给“self”中的“slider”

我做什么?谢谢!

最佳答案

我对代码做了一些修改,因此它可以编译,尽管它确实改变了一些含义。

class SliderControllerView: UIView {

// Converted "let" to "var"
private var type: ControlType!
private var label: UILabel!
private var slider: UISlider!
private weak var delegate: SliderControllerDelegate?

private var defaults: SliderDefaults!

// Added frame to init
convenience init(frame: CGRect, type: ControlType, defaults: SliderDefaults, delegate: SliderControllerDelegate?) {

// Call designated init(frame: CGRect) for UIView
self.init(frame: frame)
self.type = type
self.delegate = delegate
self.defaults = defaults

setTranslatesAutoresizingMaskIntoConstraints(false)

label = UILabel()
configureLabel()

slider = UISlider()
configureSlider(slider)

SliderDefaults.set(slider, defaults: defaults)

if let valueFromDetauls = userDefaultsValue {
slider.value = Float(valueFromDetauls)
}

saveValueInUserDefaults()
updateLabel()
}

关于swift - 错误 : Cannot assign to '...' in 'self' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660817/

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