我有这段代码
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()
}
错误讯息:
我做什么? 谢谢!
答案 0 :(得分:3)
我对代码做了一些修改,所以它会编译,虽然它确实改变了一些意思。
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()
}
答案 1 :(得分:2)
您无法在swift中设置let属性的值,因此请将它们设置为var属性。