How do I get the emoji Title and Description to show from array?

Here is my current code:



import SwiftUI



struct ContentView: View {

    @State private var title = randomEmojiImage.randomElement() ?? "👋"

    @State private var titlePlace = Text("")

    @State private var descript = Text("\(switching())")

    

    

    

    func switching(){

        if title == "👋" {

                let titlePlace = Text("\(wave.Title)")

                let descript = Text("\(wave.Description)")

        } else if title == "✌️" {

               let titlePlace = Text("\(peace.Title)")

               let  descript = Text("\(peace.Description)")

        } else if title == "😀" {

                let titlePlace = Text("\(happy.Title)")

                let descript = Text("\(happy.Description)")

        } else if title == "💩" {

                let titlePlace = Text("\(****.Title)")

                let descript = Text("\(****.Description)")

        } else if title == "😢" {

                let titlePlace = Text("\(sad.Title)")

                let descript = Text("\(sad.Description)")

            }

            

        }

    

    





    var body: some View {

        VStack {

            

                Text("\(title)")

                    .font(.system(size: 80))

                         .padding()

            

                Text("\(titlePlace)")

                .padding()

                           

                Text("\(descript)")

                

            

           

        }

     

    }

}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}

I am trying to get the title to switch from the arrays I have created for the emojis. but it keeps throwing an error when I interpolate the switching inside the text field? what is the correct way to do this?

FYI: Newbie.

Answered by Claude31 in 695241022

When you paste code, please use Paste and Match Style to avoid all the extra lines.

In addition, you should provide all the elements to allow to test code.

  • how is randomEmojiImage defined ?
  • what is wave ?

it keeps throwing an error

Please tell exactly which error, and where precisely.

struct ContentView: View {
    @State private var title = randomEmojiImage.randomElement() ?? "👋"
    @State private var titlePlace = Text("")
    @State private var descript = Text("\(switching())")

    func switching(){

        if title == "👋" {
                let titlePlace = Text("\(wave.Title)")
                let descript = Text("\(wave.Description)")

        } else if title == "✌️" {
               let titlePlace = Text("\(peace.Title)")
               let  descript = Text("\(peace.Description)")

        } else if title == "😀" {

                let titlePlace = Text("\(happy.Title)")
                let descript = Text("\(happy.Description)")

        } else if title == "💩" {

                let titlePlace = Text("\(****.Title)")
                let descript = Text("\(****.Description)")

        } else if title == "😢" {

                let titlePlace = Text("\(sad.Title)")
                let descript = Text("\(sad.Description)")

            }

        }

    var body: some View {

        VStack {
                Text("\(title)")
                    .font(.system(size: 80))
                         .padding()

                Text("\(titlePlace)")
                .padding()

                Text("\(descript)")

        }

    }

}


struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView1()

    }

}

There are plenty of problems here.

  1. Go and learn how to use @State var.

  2. Text() is a view, not a String: you cannot assign it to titlePlace

  3. You cannot call switching() to initialise the var ; this call should be done when you elsewhere (when do you want to switch ? When hitting a button ?)

  4. take care that var should start with lowerCase. And cannot be ****

This is a partially corrected code, but I miss information to complete.

struct Behavior {
    var title = ""
    var description = ""
}

struct ContentView1: View {
    
    let randomEmojiImage = ["👋", "✌️", "😀", "💩", "😢"]
    @State private var title = "👋" <:: randomEmojiImage.randomElement() ?? "👋"
    @State private var titlePlace = ""
    @State private var descript = ""

    func switching(){

        let wave = Behavior(title: "wave", description: "wave")
        let peace = Behavior(title: "peace", description: "peace")
        let happy = Behavior(title: "happy", description: "happy")
        let stars = Behavior(title: "****", description: "****")
        let sad = Behavior(title: "sad", description: "sad")

        if title == "👋" {
                let titlePlace = wave.title
                let descript = wave.description

        } else if title == "✌️" {
               let titlePlace = peace.title
               let  descript = peace.description

        } else if title == "😀" {

                let titlePlace = happy.title
                let descript = happy.description

        } else if title == "💩" {

                let titlePlace = stars.title
                let descript = stars.description

        } else if title == "😢" {

                let titlePlace = sad.title
                let descript = sad.description

            }

        }

Accepted Answer

When you paste code, please use Paste and Match Style to avoid all the extra lines.

In addition, you should provide all the elements to allow to test code.

  • how is randomEmojiImage defined ?
  • what is wave ?

it keeps throwing an error

Please tell exactly which error, and where precisely.

struct ContentView: View {
    @State private var title = randomEmojiImage.randomElement() ?? "👋"
    @State private var titlePlace = Text("")
    @State private var descript = Text("\(switching())")

    func switching(){

        if title == "👋" {
                let titlePlace = Text("\(wave.Title)")
                let descript = Text("\(wave.Description)")

        } else if title == "✌️" {
               let titlePlace = Text("\(peace.Title)")
               let  descript = Text("\(peace.Description)")

        } else if title == "😀" {

                let titlePlace = Text("\(happy.Title)")
                let descript = Text("\(happy.Description)")

        } else if title == "💩" {

                let titlePlace = Text("\(****.Title)")
                let descript = Text("\(****.Description)")

        } else if title == "😢" {

                let titlePlace = Text("\(sad.Title)")
                let descript = Text("\(sad.Description)")

            }

        }

    var body: some View {

        VStack {
                Text("\(title)")
                    .font(.system(size: 80))
                         .padding()

                Text("\(titlePlace)")
                .padding()

                Text("\(descript)")

        }

    }

}


struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView1()

    }

}

There are plenty of problems here.

  1. Go and learn how to use @State var.

  2. Text() is a view, not a String: you cannot assign it to titlePlace

  3. You cannot call switching() to initialise the var ; this call should be done when you elsewhere (when do you want to switch ? When hitting a button ?)

  4. take care that var should start with lowerCase. And cannot be ****

This is a partially corrected code, but I miss information to complete.

struct Behavior {
    var title = ""
    var description = ""
}

struct ContentView1: View {
    
    let randomEmojiImage = ["👋", "✌️", "😀", "💩", "😢"]
    @State private var title = "👋" <:: randomEmojiImage.randomElement() ?? "👋"
    @State private var titlePlace = ""
    @State private var descript = ""

    func switching(){

        let wave = Behavior(title: "wave", description: "wave")
        let peace = Behavior(title: "peace", description: "peace")
        let happy = Behavior(title: "happy", description: "happy")
        let stars = Behavior(title: "****", description: "****")
        let sad = Behavior(title: "sad", description: "sad")

        if title == "👋" {
                let titlePlace = wave.title
                let descript = wave.description

        } else if title == "✌️" {
               let titlePlace = peace.title
               let  descript = peace.description

        } else if title == "😀" {

                let titlePlace = happy.title
                let descript = happy.description

        } else if title == "💩" {

                let titlePlace = stars.title
                let descript = stars.description

        } else if title == "😢" {

                let titlePlace = sad.title
                let descript = sad.description

            }

        }

How do I get the emoji Title and Description to show from array?
 
 
Q