0

I have created a New View in my App, but the Identifiable Object won't append to the Array. I really don't know why its not appending...

Here is the Code:

struct FirstSettingsIdentifiables: Identifiable {
    var id: UUID = UUID()
    
    var name: String
    var icon: String
}
struct SettingsView: View {
    
    @EnvironmentObject var settingItems: ContentModel
    
    @State var firstArr: [FirstSettingsIdentifiables] = []
    
    init() {
        createFirstList()
        print("Settings successfully initialized.")
    }
    
    var body: some View {
        
        return VStack {
            Text("Einstellungen")
                .font(.title)
            
            NavigationView {
                //Mitteilungen Liste
                List(firstArr) { x in
//                    ForEach(firstArr) { x in
    //                    VStack {
    //                        Image(systemName: x.icon)
                            Text("Das ist ein test")
    //                    }
//                    }
                }.navigationBarTitle("Mitteilungen")
            }
        }
    }
    
    func createFirstList() {
        
        let aText = "Mitteilungen"
        let aIcon = "info.circle.fill"
        let aObject = FirstSettingsIdentifiables(name: aText, icon: aIcon)
        
        firstArr.append(aObject)
        
        print(firstArr.count)
        
    }
    
    
}

The problem is probably in the createFirstList() Section. In this function, the Object aObject is full of data(This is working fine), but then the Object won't append to my firstArr. The count is always 0.

What am I doing wrong here?

1 Answer 1

2

You are changing the value of firstArr too early. Instead of calling createFirstList() in the init, remove that and instead add the following code onto the view body:

VStack {
    /* ... */
}
.onAppear(perform: createFirstList)

Alternatively, you could do the following:

init() {
    _firstArr = State(initialValue: getFirstList())
    print(firstArr.count)
    print("Settings successfully initialized.")
}

/* ... */

func getFirstList() -> [FirstSettingsIdentifiables] {
    let aText = "Mitteilungen"
    let aIcon = "info.circle.fill"
    let aObject = FirstSettingsIdentifiables(name: aText, icon: aIcon)
    return [aObject]
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well it works, BUT if I click on another View and then go back to this View, it executes the "createFirstList" function again. And the second Method cannot be used because I have more then 1 object to append in the future.
I added a Bool variable that sets to false after createFirstList gets executed and then don't executes again. But I don't think that this is the best way to solve it! Is there another way to do it?
@yama_HD I'm not sure what you mean. What is the reason for having your Boolean variable?
To Check if onAppear was already executed. Because its getting executed every time I open the view and then I have all variables twice
@yama_HD In that case, instead of firstArr.append(aObject), do firstArr = [aObject]. That should fix your issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.