0
有没有什么办法来限制在任何地方,但函数设置类内的属性?这里是代码来说明问题:拒绝属性编辑从里面的任何地方,但功能
import UIKit
class MyViewController: UIViewController {
var property: Int?
func setProperty(_ property: Int?, animated: Bool,
withColor color: UIColor,
andSomethingElse something: Any) {
// only allow property to be set here
self.property = property
// ...
}
override func viewDidLoad() {
super.viewDidLoad()
// What I'd like to achieve is deny following...
property = 42
// ...and somehow force to set value exclusively via function
setProperty(42,
animated: true,
withColor: .green,
andSomethingElse: LongCat())
}
}