尝试在函数中返回不同的形状。错误:函数声明了不透明的返回类型

时间:2020-10-07 14:17:20

标签: swift swiftui

我试图让一个函数根据switch语句返回不同类型的Shape。这是代码...谢谢Function declares an opaque return type, but the return statements in its body do not have matching underlying types. The return functions

图片中的代码:

func getShape(_ card: SetModel.Card) -> some Shape {
        switch card.shape {
        case .oval:
            return ovalShape()
        case .dimond:
            return dimondShape()
        case .squiggle:
            return squiggleShape()
        }
    }

struct ovalShape: Shape {
    func path(in rect: CGRect) -> Path {
        let path = Path(
            roundedRect: CGRect(x: rect.midX - ((rect.width * 2.5) / 2),
                                y: rect.midY - (rect.width / 2),
                                width: rect.width * 2.5,
                                height: rect.width),
            cornerRadius: 200)
        return path
    }
}

struct dimondShape: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        let startPoint = CGPoint(x: rect.midX, y: rect.midY - (rect.width / 5))
        let leftMidPoint = CGPoint(x: rect.minX, y: rect.midY)
        let bottomPoint = CGPoint(x: rect.midX, y: rect.midY + (rect.width / 5))
        let rightMidPoint = CGPoint(x: rect.maxX, y: rect.midY)
        
        path.move(to: startPoint)
        path.addLine(to: leftMidPoint)
        path.addLine(to: bottomPoint)
        path.addLine(to: rightMidPoint)
        
        return path
    }
}

0 个答案:

没有答案