【问题标题】:Error: Value of type 'some View' has no member 'stroke'错误:“某些视图”类型的值没有成员“中风”
【发布时间】:2020-12-03 18:22:02
【问题描述】:

我关注Stanfords' CS193p Developing Apps for iOS online course。 我正在使用 Xcode 11.5。 (我没有更新,因为这是课程讲师(Paul Heagarty)正在使用的版本。)

我正在尝试执行Assignment 3 (Set Game)。目前(为了避免重复代码)我正在尝试替换这个片段:

            VStack {
                ForEach(0..<numberOfShapes) { index in
                    if self.card.shape == .diamond {
                        ZStack {
                            Diamond().fill()
                                .opacity(self.opacity)
                            Diamond().stroke(lineWidth: self.shapeEdgeLineWidth)
                        }
                    }
                    if self.card.shape == .squiggle {
                        ZStack {
                            Rectangle().fill()
                            Rectangle().stroke(lineWidth: self.shapeEdgeLineWidth)
                        }
                    }
                    if self.card.shape == .oval {
                        ZStack {
                            Ellipse().fill()
                            Ellipse().stroke(lineWidth: self.shapeEdgeLineWidth)
                        }
                    }
                }
            }

有了这个片段:

            VStack {
                ForEach(0..<numberOfShapes) { index in
                    ZStack {
                        shape(self.card.shape).opacity(self.opacity)
                        shape(self.card.shape).stroke(lineWidth: 2.5) // ERROR here: Value of type 'some View' has no member 'stroke'
                    }
                }
            }

还有这个@ViewBuilder 函数:

@ViewBuilder
func shape(_ shape: SetGameModel.Card.Shape) -> some View {
    if shape == .diamond {
        Diamond()
    } else if shape == .squiggle {
        Rectangle()
    } else {
        Ellipse()
    }
}

这里是完整的查看代码:

import SwiftUI

struct SetGameView: View {
    @ObservedObject var viewModel: SetGameViewModel
    
    var body: some View {
        Grid(viewModel.cards) { card in
                CardView(card: card).onTapGesture {
                    self.viewModel.choose(card: card)
                }
                .padding(5)
            }
        .padding()
        .foregroundColor(Color.orange)
    }
}

struct CardView: View {
    var card: SetGameModel.Card

    var numberOfShapes: Int {
        switch card.numberOfShapes {
        case .one:
            return 1
        case .two:
            return 2
        case .three:
            return 3
        }
    }
        
    var opacity: Double {
        switch card.shading {
        case .open:
            return 0.0
        case .solid: // filled
            return 1.0
        case .striped: // you can use a semi-transparent color to represent the “striped” shading.
            return 0.33
        }
    }
    
    var color: Color {
        switch card.color {
        case .green:
            return Color.green
        case .purple:
            return Color.purple
        case .red:
            return Color.red
        }
    }
    
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: cornerRadius).fill(Color.white)
            RoundedRectangle(cornerRadius: cornerRadius)
                .stroke(lineWidth: card.isChosen ? chosenCardEdgeLineWidth : normalCardEdgeLineWidth)
                .foregroundColor(card.isChosen ? Color.red : Color.orange)
            VStack {
                ForEach(0..<numberOfShapes) { index in
                    ZStack {
                        shape(self.card.shape).opacity(self.opacity)
                        shape(self.card.shape).stroke(lineWidth: 2.5) // ERROR here: Value of type 'some View' has no member 'stroke'
                    }
                }
            }
            .foregroundColor(self.color)
            .padding()
        }
        .aspectRatio(cardAspectRatio, contentMode: .fit)
    }
    
    // MARK: - Drawing Constants
    let cornerRadius: CGFloat = 10.0
    let chosenCardEdgeLineWidth: CGFloat = 6
    let normalCardEdgeLineWidth: CGFloat = 3
    let shapeEdgeLineWidth: CGFloat = 2.5
    let cardAspectRatio: CGSize = CGSize(width: 2, height: 3) // 2/3 aspectRatio
}

@ViewBuilder
func shape(_ shape: SetGameModel.Card.Shape) -> some View {
    if shape == .diamond {
        Diamond()
    } else if shape == .squiggle {
        Rectangle()
    } else {
        Ellipse()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        SetGameView(viewModel: SetGameViewModel())
    }
}

我有这个错误:

Value of type 'some View' has no member 'stroke'

我想不通,怎么了?我该如何解决?

请尝试以某种方式帮助我修复它,我作为初学者会理解 ????

顺便说一下,Diamond() 是我的自定义形状(如 Rectangle())。 如果您还需要 ViewModel、Model 或其他一些文件来帮助我修复它,请告诉我:-)

【问题讨论】:

  • 这给了我这个错误:全局函数'shape'的返回类型要求'_ConditionalContent<_conditionalcontent rectangle>, Ellipse>'符合'Shape'

标签: swift swiftui cs193p


【解决方案1】:

stroke 是在 Shape 上定义的,而不是在 View 上定义的,您返回的是 Shapes,而不仅仅是 Views。需要将shape的返回类型改为some Shape

遗憾的是@ViewBuilder 需要返回类型为some View,而不是some Shape,因此您需要删除@ViewBuilder 属性并确保您的函数从每个分支返回相同的Shape。为此,您可以实现一个类型擦除的Shape,称为AnyShape,类似于AnyView for View,并返回每个Shape 的类型擦除版本。

struct AnyShape: Shape {
    init<S: Shape>(_ wrapped: S) {
        _path = { rect in
            let path = wrapped.path(in: rect)
            return path
        }
    }

    func path(in rect: CGRect) -> Path {
        return _path(rect)
    }

    private let _path: (CGRect) -> Path
}

func shape(_ shape: SetGameModel.Card.Shape) -> some Shape {
    if shape == .diamond {
        return AnyShape(Diamond())
    } else if shape == .squiggle {
        return AnyShape(Rectangle())
    } else {
        return AnyShape(Ellipse())
    }
}

【讨论】:

  • 这给了我这个错误:全局函数'shape'的返回类型要求'_ConditionalContent<_conditionalcontent rectangle>, Ellipse>'符合'Shape'
  • @user14119170 检查我更新的答案,现在代码编译得很好
猜你喜欢
  • 2021-12-10
  • 2020-03-30
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多