【发布时间】:2020-06-21 20:14:56
【问题描述】:
我的 SwiftUI 代码中有这个功能
func getShape(shape:Int, i: Int) -> some View {
switch self.selectedShape {
case 0:
return Rectangle()
.stroke(Color.pink)
.frame(width: 150, height: 150)
.position(CGPoint(x: scrCenterX, y: scrCenterY))
.rotationEffect(Angle(degrees: Double(i) * Double(self.angleStep)))
case 1:
return Circle()
.stroke(Color.pink)
.frame(width: 150, height: 150)
.position(CGPoint(x: scrCenterX, y: scrCenterY))
.rotationEffect(Angle(degrees: Double(i) * Double(self.angleStep)))
case 2:
return Capsule()
.stroke(Color.pink)
.frame(width: 150, height: 150)
.position(CGPoint(x: scrCenterX, y: scrCenterY))
.rotationEffect(Angle(degrees: Double(i) * Double(self.angleStep)))
default:
return Rectangle()
.stroke(Color.pink)
.frame(width: 150, height: 150)
.position(CGPoint(x: scrCenterX, y: scrCenterY))
.rotationEffect(Angle(degrees: Double(i) * Double(self.angleStep)))
}
}
并且编译器说“函数声明了一个不透明的返回类型,但它的主体中的返回语句没有匹配的底层类型”。如果我将所有“案例”形状更改为例如Circle 然后代码编译没有任何错误。我如何根据 selectedShape 值冷实现返回形状? 谢谢。
【问题讨论】:
标签: swiftui