0

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()
            }
        }
    }

2 Answers2

1

You can't access other properties before they are initialized. Just add lazy in front of the desired properties. The difference is that they will not initialize until being accessed.

lazy var flags = emojis.keys
lazy var names = emojis.values
narek.sv
  • 845
  • 5
  • 22
0

You cannot access other instance properties (in your particular case, emojis) to initialise instance properties.

You can simply change flags and names to be computed properties to resolve the issue.

var keys: Dictionary<String, String>.Keys {
  dict.keys
}

var values: Dictionary<String, String>.Values {
  dict.values
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116