ufookoro
ufookoro

Reputation: 373

ForEach in SwiftUI Warning Issue

I have been using SwiftUI for a few months now an I am having difficulty with using ForEach. I am aware that the ForEach protocol demands a unique identifier, but I have used /.self to overcome that aspect of the protocol. Now unit testing a ForEach statement but I am getting a warning which is preventing build.

Warning is Result of 'ForEach' initializer is unused

import SwiftUI

struct GetdOrderView: View {

@State private var myFamily = ["Ufuoma","Efe","David","Vicky","Beth"]


    //The use of ForEach
    func myForachOne() {

        ForEach((0 ... myFamily.count), id: \.self) {member in

            VStack {
            Text("\(member)")
        }

        }
    }


    var body: some View {

    Text("Hello world")


    }        

}

Upvotes: 2

Views: 273

Answers (2)

Hardik Bar
Hardik Bar

Reputation: 1760

//Use This

import SwiftUI

    struct GetdOrderView: View {

    @State private var myFamily = ["Ufuoma","Efe","David","Vicky","Beth"]


        //The use of ForEach
        func myForachOne() -> some View {

            ForEach((0 ... myFamily.count), id: \.self) {member in

                VStack {
                Text("\(member)")
            }

            }
        }


        var body: some View {

        Text("Hello world")


        }        

    }

Upvotes: 1

Asperi
Asperi

Reputation: 257493

Instead of

func myForachOne() {

Use

func myForachOne() -> some View {

Upvotes: 1

Related Questions