मैं Stanfords' CS193p IOS ऑनलाइन पाठ्यक्रम के लिए विकासशील ऐप्स का अनुसरण कर रहा हूं। मैं एक्सकोड 11.5 का उपयोग कर रहा हूँ। (मैंने अपडेट नहीं किया क्योंकि यह वह संस्करण है जिसका पाठ्यक्रम प्रशिक्षक (पॉल हेगार्टी) उपयोग कर रहा है।)
मैं असाइनमेंट करने का प्रयास कर रहा हूं 3 (सेट गेम)। वर्तमान में (डुप्लिकेटिंग कोड से बचने के लिए) मैं इस टुकड़े को बदलने की कोशिश कर रहा हूं:
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'
मैं इसका पता नहीं लगा सकता, क्या गलत है? मेरे द्वारा यह कैसे किया जा सकता है?
कृपया इसे किसी तरह से ठीक करने में मेरी मदद करने का प्रयास करें, जिसे मैं एक शुरुआत के रूप में समझूंगा 🙏
हीरा () मेरा कस्टम आकार है (जैसे आयत ())। अगर आपको इसे ठीक करने में मेरी सहायता के लिए व्यूमोडेल, मॉडल या कुछ अन्य फाइलों की भी आवश्यकता है, तो मुझे बताएं :-)
1 उत्तर
stroke
को Shape
पर परिभाषित किया गया है, न कि View
पर और आप Shape
को वापस कर रहे हैं, न कि केवल View
को। आपको shape
के रिटर्न प्रकार को some Shape
में बदलना होगा।
अफसोस की बात है कि @ViewBuilder
को रिटर्न प्रकार some View
होना चाहिए, न कि some Shape
, इसलिए आपको @ViewBuilder
विशेषता को हटाने की जरूरत है और सुनिश्चित करें कि आपका फ़ंक्शन वही लौटाता है Shape
प्रत्येक शाखा से। इसे प्राप्त करने के लिए, आप View
के लिए AnyView
के समान AnyShape
नामक टाइप-मिटा हुआ Shape
लागू कर सकते हैं और प्रत्येक 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())
}
}