Learn Courses My Dashboard

Module 2: Challenge 4

So I just started on Challenge 4 of Module 2 and I am getting the following errors. Cannot find ‘action’ in scope, consecutive statements on a line must be separated by ‘;’
Insert ‘;’
Expected expression

Any Advice?

//
//  ContentView.swift
//  Shared
//
//  Created by Eric Beecroft on 10/6/21.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Button("1"){ action: numberList = Integer.random(1...10)
                List(numberList in array)
            }
            
            Button("2"){ action: //Increase all numbers when tapped
                List(numberList in array ++)
                
            }
            Button("3"){action: numberList.removeAll()
                
            }
        }
    }
}

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

Your button can look like this instead of how you have it written

Button {
// action code here
} label: {
Text(“1”)
}

Change:

to:

Button("1") {
    numberList = Int.random(in: 1...10)
}
  1. Integer is not a type in Swift; Int is.
  2. What are you doing with this line: List(numberList in array)? You can’t have a List inside tha action closure of a Button and where does array come from?

Charles,

You have a number of active threads in the Code Crew community. One each for Module 2 challenges 2, 3 and 4 and a couple of others for the 14 day Challenge. I would recommend that you deal with one at a time because the understanding that you gain in one challenge has a bearing on the next.

With reference to the above code, you can’t place a List() inside a button.

The challenge says to create a UI with a List and 3 Buttons below it, so in very basic terms it should look like this:

VStack {

    List 

    Button

    Button

    Button

}

I decided to reduce the code down to a few buttons. Right now I am getting the error message that the numberList is out of scope.

//
//  ContentView.swift
//  Shared
//
//  Created by Eric Beecroft on 10/6/21.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Button("1"){ numberList = Int.random(1...10)
            }
            
            Button("2"){
                
            }
            Button("3"){
                
            }
        }
    }
}

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

That is something I didn’t know, I thought that was something you can do in Swift.

Consider changing it to a let

struct ContentView: View {
    var body: some View {
        VStack{
            Button("1") { 
                let numberList = Int.random(1...10)
            }
            
            Button("2"){
                
            }
            Button("3"){
                
            }
        }
    }
}

I added the let statement and I am now getting an error message that reads “Missing argument label ‘in:’ in call”
Any advice?

//
//  ContentView.swift
//  Shared
//
//  Created by Eric Beecroft on 10/6/21.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Button("1"){
                let numberList = Int.random(1...10)
            }
            
            Button("2"){
                
            }
            Button("3"){
                
            }
        }
    }
}

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

Did you try clicking on the fix it offered by Xcode? That will often correct the error for you.

In this case, Int.random(1...10) should be Int.random(in: 1...10)

1 Like

It fixed it. I looked at the answer but I don’t know how they came up with it. I am kind of struggling to understand the answer.

import SwiftUI

struct ContentView: View {
    
    // Stores our generated numbers
    @State var numbers = [Int]()
    
    var body: some View {
        
        VStack {
            
            // For each number, create a text element
            List(numbers, id: \.self) { num in
                
                Text(String(num))
            }
            HStack(spacing: 10.0) {
                Button("Generate") {
                    
                    // Declare a variable outside the scope of the loop
                    var randNumber = 0
                    
                    // Loop
                    repeat {
                        
                        // Randomize a number
                        randNumber = Int.random(in: 1...10)
                        
                        // Add it to our array
                        numbers.append(randNumber)
                        
                        // If it's not a 7, then loop
                    } while randNumber != 7
                    
                }
                .padding()
                .background(Color.blue)
                .cornerRadius(10)
                
                Button("Add +1") {
                    
                    // Check if there are items in the array
                    if numbers.count == 0 {
                        
                        // The return keyword will cause execution to stop and return (skipping all the code below the return keyword)
                        return
                    }
                    
                    // Loop through the array
                    for index in 0...numbers.count-1 {
                        
                        // Increment it by 1
                        numbers[index] += 1
                    }
                }
                .padding()
                .background(Color.blue)
                .cornerRadius(10)
                
                Button("Clear") {
                    numbers.removeAll()
                }
                .padding()
                .background(Color.blue)
                .cornerRadius(10)
            }
            .foregroundColor(.white)
            
        }
        
    }
}

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

What don’t you understand?