1

Im trying to loop through an array of class objects within my view. Im getting an error I don't understand or can't seem to google through. Im just trying to loop over the galaxyPlanet and then display the planet.planetName. My swiftUI knowledge has hit its limits here and I can't seem to google the answer. The errors it's giving me are:

Cannot use instance member 'galaxy' within property initializer; property initializers run before 'self' is available

Cannot convert value of type '[Any]' to expected argument type 'Binding<C>'

Generic parameter 'C' could not be inferred

Initializer 'init(_:)' requires that 'Binding<Subject>' conform to 'StringProtocol'

None of these error message make sense and they are all showing within the PlanetListView File.

PlanetListView File:

struct PlanetListView: View {
  
  @Binding var player: Player
  @Binding var galaxy: Galaxy
  
  var galaxyPlanets:[Any] = galaxy.galaxyPlanets
  
  var body: some View {
    ZStack {
      Color.black.ignoresSafeArea()
      HStack {
        VStack {
          ForEach(galaxyPlanets) { planet in
            Text(planet.planetName)
              .fontWeight(.bold)
              .font(.title3)
              .foregroundColor(.white)
          }
        }
        Spacer()
      }
    }
  }
}

Galaxy Class File:

class Galaxy {
  var numPlanets: Int = 0;
  var galaxyPlanets: [Any] = [];
  
  init() {
    self.numPlanets = generatePlanets()
    
    buildPlanetArray()
  }
  
  func generatePlanets() -> Int {
    numPlanets = Int.random(in: 0..<6)
    return Int(numPlanets)
  }
  
  func buildPlanetArray(){
    for _ in 1...numPlanets {
      galaxyPlanets.append(Planet())
    }
  }
}

Planet class file:

class Planet {
  var planetName: String = "";
  
  init() {
    self.planetName = generatePlanetName()
  }
  
  func generatePlanetName() -> String {
    func randomString(length: Int) -> String {
      let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      return String((0..<length).map{ _ in letters.randomElement()! })
    }
    
    func randomNumber(length: Int) -> String {
      let numbers = "1234567890"
      return String((0..<length).map{ _ in numbers.randomElement()! })
    }
    
    var generatedName = randomString(length: 3);
    
    generatedName = generatedName + "-" + randomNumber(length: 5);
    return generatedName;
  }
}
Charles L.
  • 1,844
  • 2
  • 34
  • 56
  • Does this answer your question? [SwiftUI @State var initialization issue](https://stackoverflow.com/questions/56691630/swiftui-state-var-initialization-issue) . Also have a look at this link: https://stackoverflow.com/questions/68533137/cannot-use-instance-member-videoname-within-property-initializer-property-ini and https://stackoverflow.com/questions/68434869/how-can-i-initialize-a-state-var-in-swift-ui-without-getting-the-error-cannot-u – workingdog support Ukraine Sep 25 '22 at 05:25
  • you don't need `var galaxyPlanets:[Any] = galaxy.galaxyPlanets`, just use `galaxy.galaxyPlanets` directly, like: `ForEach(galaxy.galaxyPlanets) {...}`. And I suggest you use `struct` for `Galaxy` and `Planet`, not classes. Also use `var galaxyPlanets: [Planet] = []` in `Galaxy` and remove all `;`. – workingdog support Ukraine Sep 25 '22 at 05:33

0 Answers0