Use of a Toggle Switch

I have been trying to create an app based on Module 1 using a Toggle Switch to use two different assets like flashcards for numbers and alphabets. I am getting tied up in knots. I am attaching a copy of the program to get some advice from you.
import SwiftUI

struct ContentView: View {
@State var playerCard = “card1”
@State var toggleIsOn: Bool = false
@State var alphabet = “acard1”
var body: some View {

    ZStack {
        
        Image("background-plain")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .ignoresSafeArea()
        
        VStack(spacing: 12.0) {
            
            
            HStack {
                Spacer()
                Image("KidsPicture")
                    .renderingMode(.original)
                    .resizable(capInsets: EdgeInsets(top: 0.0, leading: 0.0, bottom: 0.0, trailing: 0.0), resizingMode: .stretch)
                    .aspectRatio(contentMode: .fit)
                Spacer()
                
                    .frame(width: 15)
                
                
                Text(toggleIsOn ? "NUMBERS" : "ALPHABET")
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
                Toggle(isOn: $toggleIsOn, label: {
                    
                })
                
            }
            if (toggleIsOn) {
                Image(playerCard)
                    .frame(width: 430.0)
                    .cornerRadius(50)
              }
            
            
            
        
            Spacer()
            
            Button("PRESS") {
                press()
            }
            .padding()
            .foregroundColor(.white)
            
            
            .fontWeight(/*@START_MENU_TOKEN@*/.heavy/*@END_MENU_TOKEN@*/)
            .background(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=View@*/Color.orange/*@END_MENU_TOKEN@*/)
            .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/3/*@END_MENU_TOKEN@*/)
            .cornerRadius(/*@START_MENU_TOKEN@*/6.0/*@END_MENU_TOKEN@*/)
            
            .buttonBorderShape(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=shape: ButtonBorderShape@*/.automatic/*@END_MENU_TOKEN@*/)
            .accessibilityLabel(/*@START_MENU_TOKEN@*/"Label"/*@END_MENU_TOKEN@*/)
            .font(/*@START_MENU_TOKEN@*/.headline/*@END_MENU_TOKEN@*/)
            
            Spacer()
            
            
        }
        Spacer()
        Spacer()
        Spacer()
        Spacer()
    }
    
}
    
    
    
    
    
    func press() {
        //randomize the card
        
        
        let playerCardValue = Int.random(in: 1...5)
        playerCard = "card1" + String(playerCardValue)
        
        }
        
    }

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

Can you explain what the issue is??

Also Al l the things that say “token” mean you haven’t gotten rid of the auto complete filled in example

Hello Michaela,
I had tried to use a toggle switch to change the assets from numbers to letters bases on the learnings of Mod1. When toggle is “on” the numbers random selection is working but when toggle is "off " the letters don’t work also the layout changes and I cannot see the toggle switch. Should I be making a different layout?
Thank You,
Jaime

Not sure what you mean by the above

She just means all the things that say “TOKEN” (/*@START_MENU_TOKEN@*/) need to be fixed/removed. These are placeholders in Xcode.

So, for example, this:
.fontWeight(/*@START_MENU_TOKEN@*/.heavy/*@END_MENU_TOKEN@*/)
Should be this:
.fontWeight(.heavy)

1 Like

Hello Mark, Thanks, I think that these tokens came about when I copied and paste the program to your page. I will have to find a better way to do so.
I have been out of touch with Swift due to my frequent travel schedule.
Jaime





Hello Mark, Copy and Paste still introduces the Tokens. So now I have taken screen shots of the program. Is there a better way of copying/pasting?
Thanks for your help. JAime

Tokens:

Screenshot 2023-05-13 at 8.17.15 AM
These are your tokens in code.

The purpose of a token is to allow you to easily use TAB to navigate to them in the Xcode editor and change them.

If you don’t want to change them and stay with the default values, you can DOUBLE-CLICK the highlighted code (where the light blue background is).

The token will be removed (you can no longer TAB to them) and the code will no longer be highlighted.

And you need to supply actual values for background and buttonBorderShape because currently those placeholder tokens don’t have a default value like the others do but instead show you what type of data needs to go there.

1 Like

//
// ContentView.swift
// MIL4 2023
//
// Created by Jaime Fernandes on 25/4/2023.
//

import SwiftUI

struct ContentView: View {
@State var playerCard = “card1”
@State var toggleIsOn: Bool = false
@State var letters = “acard1”
var body: some View {

    ZStack {
        
        Image("background-plain")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .ignoresSafeArea()
        
        VStack(spacing: 12.0) {
            
            Spacer()
            HStack {
                Spacer()
                Image("KidsPicture")
                
                    .resizable()
                
                    .aspectRatio(contentMode: .fit)
                Spacer()
                
                    .frame(width: 15)
                
                    .padding()
                Text(toggleIsOn ? "NUMBERS" : "LETTERS")
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
                Toggle(isOn: $toggleIsOn, label: {
                    Spacer()
                })
            }
            Spacer()
            
            if (toggleIsOn) {
                Image(playerCard)
                    .frame(width: 430.0)
                    .cornerRadius(50)
            }
            
            Spacer()
            Spacer()
            Spacer()
            
            Button("PRESS") {
                press()
            }
            .padding()
            .foregroundColor(.white)
            
            .background(.orange)
            .border(Color.black, width: 3)
            .cornerRadius(6.0)
            
    
            .accessibilityLabel("Label")
            .font(.headline)
            Spacer()
            Spacer()
            
        }
    }
    Spacer()
}


func press() {
    //randomize the card
    
    
    let playerCardValue = Int.random(in: 1...18)
    playerCard = "card1" + String(playerCardValue)
}

}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hello Roosterboy,
I have deleted the tokens. I am trying to develop a Flasher App where I can select with a Toggle Switch either numbers or letters. This Is adapted from the lessons in Mod 1.
The numbers seem to be working well however the letters do not even though I have put in them in the assets. Can you help please. Thanks Jaime

Hello, I have not heard from you since my last response two days ago. Jaime