I am following Stanfords' CS193p Developing Apps for iOS online course. I am using Xcode 11.5. (I didn't update because that's the version the course instructor (Paul Heagarty) is using.)
I'm trying to do the Assignment 3 (Set Game). I know my code is probably poorly written in many places, but I am going to refine it later, right now I am just trying to make it work with no errors. The error I currently can not fix is this:
var shading: SetGameModel.Card.Shading = card.shading //Error: Cannot use instance member 'card' within property initializer; property initializers run before 'self' is available
And here is full View code: If you also need ViewModel, Model or some other files to help me fix it, let me know :-)
Please try to help me fix it in some way, that I will understand as a beginner ????
//
// SetGameView.swift
// TheSetGame
//
import SwiftUI
struct SetGameView: View {
@ObservedObject var viewModel: SetGameViewModel
var body: some View {
Grid(viewModel.cards) { card in
CardView(card: card, shading: .solid).onTapGesture {
self.viewModel.choose(card: card)
}
}
.padding()
.foregroundColor(Color.orange)
}
}
struct CardView: View {
var card: SetGameModel.Card
// Shape features (numberOfShapes, shape, shading, color):
var numberOfShapes: Int {
switch card.numberOfShapes {
case .one:
return 1
case .two:
return 2
case .three:
return 3
}
}
var shading: SetGameModel.Card.Shading = card.shading //Error: Cannot use instance member 'card' within property initializer; property initializers run before 'self' is available
var isFilled: Bool { shading == .solid ? true : false }
var isStriped: Bool { shading == .striped ? true : false }
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: 10.0).fill(Color.white)
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: card.isChosen ? 6 : 3)
.foregroundColor(card.isChosen ? Color.red : Color.orange)
VStack {
ForEach(0..<numberOfShapes) { index in
if self.card.shape == .diamond {
Diamond()//.scale(0.75)
}
if self.card.shape == .squiggle {
Rectangle()//.scale(0.75)
}
if self.card.shape == .oval {
Ellipse()//.scale(0.75)
}
}
}
.foregroundColor(self.color)
.padding()
}
.aspectRatio(2/3, contentMode: .fit)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
SetGameView(viewModel: SetGameViewModel())
}
}
var shading: SetGameModel.Card.Shading
tolazy var shading: SetGameModel.Card.Shading
work? – George