Swift 关于 willset 的 问题
本帖最后由 漆雕古代 于 2017-3-12 20:48 编辑最近在看一点swift遇到一个问题,不知道该怎么解决,代码如下
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() ->Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
class EquilateralTriangle: NamedShape {
var sideLength: Double = 0.0
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 3
}
var perimeter: Double {
get {
return 3.0 * sideLength
}
set {
self.sideLength = newValue / 3.0
}
}
override func simpleDescription() -> String {
return "An equilateral triagle with sides of length \(sideLength)."
}
}
class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
}
}
//var triangle.sideLength { // 由于用perimeter改变triangle的sideLength属性,导致重新设置的不是triangle而是triangle.sideLength
//willSet {
//print(newValue.sideLength)
//square.sideLength = newValue
//}
//didSet {
//print("triangle a change")
//}
//}
var square: Square {
willSet {
print(newValue.sideLength)
triangle.sideLength = newValue.sideLength
}
}
init(size: Double, name: String) {
self.square = Square(sideLength: size, name: name)
self.triangle = EquilateralTriangle(sideLength: size, name: name)
}
}
//第一次test
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)
//第二次test
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)
//第三次test测试setter设置的值会不会影响willSet
triangleAndSquare.triangle.perimeter = 60
print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)
输出如下
//第一次test输出结果
10.0
10.0
//第二次test输出结果
50.0
50.0
50.0
//第三次test输出结果
50.0
20.0
这段代码出自苹果的官方教程,目的在于让等边三角形和正方形的边长随时保持相等。
本来是一切ok的,但是我想在EquilateralTriangle中有一个perimeter属性拥有getter和setter的功能,可以直接更改sideLength的值,所以在这种情况下willSet不会触发,代码的目是无法达成的吧。
然后一试果然如此,自己尝试解决了一下,但是依旧不行,我自己思考出来的代码无法运行,上网搜索资料页偏少。
我就是想知道有没有可以解决这个问题的办法? 顶楼主,哈哈,祝你早日解决 看着好难啊,楼主 顶楼主,谢谢分享 回复失效了?啊? 谢谢楼主分享,哈哈 cyndiwants 发表于 2017-9-18 15:45
回复失效了?啊?
我的天啊,半年了居然有人理我了,感动 longlive 发表于 2017-9-18 21:54
谢谢楼主分享,哈哈
还没解决呢其实哈哈 感谢楼主 还有奖励马 祝解决 感谢楼主鱼币奖励 感谢楼主 这么久还没大神来解决{:10_250:}
顶楼主,谢谢分享 蒂{:5_91:{:5_91:}{:5_100:} 哈
顶 新人帮顶一发~