Cannot use instance member 'emojis' within property initializer; property initializers run before 'self' is available I have this error,would someone help me to remove it And I want to iterate through keys and value of dictionary.So one card is countries flag and other is its name.But positions of card with countries flag and card with name should appear randomly.
import SwiftUI
struct ContentView: View {
@State var emojis: [String: String] = [
"": "Andorra",
"": "Albania",
"": "Austria",
"": "Åland",
"": "Bosnia & Herzegovina",
"": "Belgium",
"": "Bulgaria",
"": "Belarus",
"": "Croatia",
"": "Cyprus",
"": "Czech Republic",
"": "Germany",
"": "Denmark",
"": "Estonia",
"": "Spain",
"": "European Union",
"": "Finland",
"": "Faroe Islands",
"": "France",
"": "Georgia",
"": "Guernsey",
"": "Gibraltar",
"": "Greece",
"": "Hungary",
"": "Ireland",
"": "Isle of Man",
"": "Iceland",
"": "Italy",
"": "Jersey",
"": "Kosovo",
"": "Liechtenstein",
"": "Lithuania",
"": "Luxembourg",
"": "Latvia",
"": "Monaco",
"": "Moldova",
"": "Montenegro",
"": "Macedonia",
"": "Malta",
"": "Netherlands",
"": "Norway",
"": "Poland",
"": "Portugal",
"": "Romania",
"": "Russia",
"": "Serbia",
"": "Switzerland",
"": "Sweden",
"": "Slovenia",
"": "Slovakia",
"": "San Marino",
"": "Turkey",
"": "United Kingdom",
"": "Ukraine",
"": "Vatican"
]
@State var emojiCount = 20
var flags = emojis.keys
var names = emojis.values
var body: some View {
NavigationView {
VStack {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 80))]) {
ForEach(emojis[0..<emojiCount], id: \.self) { emoji in
CardView(content: emoji)
.aspectRatio(2/3, contentMode: .fit)
}
}
}
.foregroundColor(.red)
Spacer()
HStack {
add
Spacer()
remove
}.font(.largeTitle)
.padding(.horizontal)
.navigationTitle("Memorize")
}
.padding(.horizontal)
.foregroundColor(.red)
}
}
var add: some View {
Button {
if emojiCount < emojis.count {
emojiCount += 1
}
} label: {
VStack {
Image(systemName: "plus.circle")
}
}
}
var remove: some View {
Button {
if emojiCount > 1 {
emojiCount -= 1
}
} label: {
VStack {
Image(systemName: "minus.circle")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
ContentView()
.preferredColorScheme(.dark)
}
}
struct CardView: View {
var content: String
@State private var isFaceUp = true
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20)
if isFaceUp {
shape.fill(.white)
shape.strokeBorder(lineWidth: 3)
Text(content)
.font(.largeTitle)
} else {
shape.fill()
}
}
.onTapGesture {
isFaceUp.toggle()
}
}
}