Error: Cannot assign to '...' in 'self'
Error: Cannot assign to '...' in 'self'
我有这个代码
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()
}
错误信息:
- 无法分配给 'self'
中的 'type'
- 无法分配给 'self'
中的 'defaults'
- 无法分配给 'self'
中的 'label'
- 无法分配给 '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中设置一个let属性的值,所以请将它们设为var属性。
我有这个代码
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()
}
错误信息:
- 无法分配给 'self' 中的 'type'
- 无法分配给 'self' 中的 'defaults'
- 无法分配给 'self' 中的 'label'
- 无法分配给 '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中设置一个let属性的值,所以请将它们设为var属性。