SwiftUI Slot Demo

Hello- I am new to developing apps but I thought I’d dive in and try the SwiftUI demo I found on YouTube. I got to the Challenge video and came up with a couple of issues that I can seem to fix. The first is a compiler error “Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type” and the second message is "Result of “ZStack"initializer is unused”. I hope
I’m in the right place to get some help :slight_smile:

@solarwinds
Welcome Carmine,
Post your code from ContentView here so that we can assist you.
You could also go back over the video and make sure that you have not missed anything.

//
// ContentView.swift
// Slots Demo
//
// Created by Carmine Loschiavo on 3/15/20.
// Copyright © 2020 Carmine Loschiavo. All rights reserved.
//

import SwiftUI

struct ContentView: View {

@State private var symbols = ["apple", "star", "cherry"]

@State private var numbers = Array(repeating: 0, count: 9)

@State private var backgrounds = Array(repeating: Color.white, count: 9)

@State private var credits = 1000
private var betAmount = 5


var body: some View {

    ZStack {
        
    
        //Background
        
       Rectangle()
        .foregroundColor(Color(red: 200/255, green: 143/255, blue: 32/255))
        .edgesIgnoringSafeArea(.all)
        
        Rectangle()
            .foregroundColor(Color(red:228/255, green: 195/255, blue: 76/255))
        .rotationEffect(Angle(degrees:45))
        .edgesIgnoringSafeArea(.all)
    
        VStack {
            
            Spacer()
            
            //title
            
            HStack {
                Image(systemName: "star.fill")
                    .foregroundColor(.yellow)
                
                Text("SwiftUI Slots")
                    .bold()
                    .foregroundColor(.red)
            
                Image(systemName: "star.fill")
                .foregroundColor(.yellow)
            }.scaleEffect(2)
            
            Spacer()
            
            //Credits counter
            Text("Credits: " + String(credits))
                .foregroundColor(.black)
                .padding(.all, 10)
                .background(Color.white.opacity(0.5))
                .cornerRadius(20)
            
            Spacer()
            
            //Cards
            VStack {
                
                 HStack {
                    Spacer()
                                   
                    CardView(symbol: $symbols[numbers[0]],
                    background:
                    $backgrounds[0])

                    CardView(symbol:
                    $symbols[numbers[1]],
                    background:
                    $backgrounds[1])
                                   
                    CardView(symbol:
                    $symbols[numbers[2]],
                    background:
                    $backgrounds[2])
                                   
                                   Spacer()
      
            }
              HStack {
                                  Spacer()
                                                 
                    CardView(symbol:
                    $symbols[numbers[3]],
                    background:
                    $backgrounds[3])

                    CardView(symbol:
                    $symbols[numbers[4]],
                    background:
                    $backgrounds[4])
                                                 
                    CardView(symbol:
                    $symbols[numbers[5]],
                    background:
                    $backgrounds[5])
                                                 
                                                 Spacer()
                
            }
                  HStack {
                                      Spacer()
                                                     
                    CardView(symbol:
                    $symbols[numbers[6]],
                    background:
                    $backgrounds[6])

                    CardView(symbol:
                    $symbols[numbers[7]],
                    background:
                    $backgrounds[7])
                                                     
                    CardView(symbol:
                    $symbols[numbers[8]],
                    background:
                    $backgrounds[8])
                                                     
                                        Spacer()
                }
        
                //Button
            Button(action: {
            }) {
                Text("Spin")
                .bold()
                .foregroundColor(.white)
                .padding(.all, 10)
                    .padding([.leading, .trailing], 30)
                .background(Color.pink)
                .cornerRadius(20)
                
            }
            
            Spacer()
            
            
             }
        
        
          }


     }
    
    
    func processResults(_ isMax:Bool = false) {
        
    
        
         //Set backgrounds back to white
        
    
        
        self.backgrounds = self.backgrounds.map({ _ in Color.white
            
    })
        
        if isMax {
            // Spin all the cards
            self.numbers = self.numbers.map({ _ in
                Int.random(in:
                0...self.symbols.count - 1)                })
        }
        else {
            //Spin the middle row
        }
                          
                          // Change the Images
                          
self.numbers[3] = Int.random(in:
    0...self.symbols.count - 1)
                          
self.numbers[4] = Int.random(in:
    0...self.symbols.count - 1)
                          
self.numbers[5] = Int.random(in:
    0...self.symbols.count - 1)
                
        //Check winning
        processWin(isMax)
           
    }
    func processWin(_ isMax:Bool = false) {
        
        var matches = 0
        
        if !isMax {
            
            //Processing for single spin
            
        }

         
        else {
            //Processing for max spin
            
            //Top row
            if self.numbers[0] == self.numbers[1] &&
            self.numbers[1] == self.numbers[2] {
                
                
                   // Won
                
                 matches += 1
                // Update backgrounds to green
                         self.backgrounds[0] = Color.green
                         self.backgrounds[1] = Color.green
                         self.backgrounds[2] = Color.green
                        
                //Middle row
                 if self.numbers[3] == self.numbers[4] &&
                           self.numbers[4] == self.numbers[5] {
                      // Won
                      matches += 1
                                     
                    // Update backgrounds to green
                        self.backgrounds[3] = Color.green
                        self.backgrounds[4] = Color.green
                        self.backgrounds[5] = Color.green
                }
                
                //Bottom row
            if self.numbers[6] == self.numbers[7] &&
                self.numbers[7] == self.numbers[8] {
                                        
                // Won
                matches += 1
                                                        
                // Update backgrounds to green
                self.backgrounds[6] = Color.green
                self.backgrounds[7] = Color.green
                self.backgrounds[8] = Color.green
            }
                
        }
        
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

}

}
}

@solarwinds
Carmine,
I can see what’s happened. The opening and closing braces have gotten out of sync which is the reason the code is failing. Let me see if I can sort that out for you and I’ll post the code back here.

OK here’s the code updated so that it works.
You still have to add code to:

  • update the credits value
  • cater for a diagonal match
  • add another button to spin the middle row only

Link to file: Link to ContentView.swift