Search code examples
swiftxcodeclassswiftuiinitializer

class initializers swift , make a vector as optional


I'm trying to give the option to the user in my app to divide the data in a custom folder.

I have created the following model describing the folder:

class FolderModel : Identifiable , Codable{

    var nameFolder : String
    var vectorData : [GenAirportModel]

    init(nameFolder: String, vectorData: [GenAirportModel] ) {
        self.nameFolder = nameFolder
        self.vectorData = vectorData
    }
}

with the following function via a textfield I add the new folder

func newFolder(name: String) {

        let newFolder: FolderModel = FolderModel(nameFolder: name, vectorData: [basicAirport])
        folderVector.append(newFolder)
        salva()
    }

The issue is, every time the user creates a new folder, the function above put inside vectorData a basic airport.

Unfortunately when the user creates the folder not yet decide what to put inside vectorData I would like that vector data be optional or empty when the user creates the new folder.

I have tried this:

class FolderModel : Identifiable , Codable{

    var nameFolder : String
    var vectorData : [GenAirportModel]?

    init(nameFolder: String, vectorData: [GenAirportModel]? = [] ) {
        self.nameFolder = nameFolder
        self.vectorData = vectorData
    }
}

and in the view that list the data I put this:

VStack{
            if addFolder {
                ADDFolder(fm: self.fm, isDiplayed: $addFolder)
            }
            NavigationView{

                List {

                    ForEach(self.fm.folderVector) { folder in

                        NavigationLink(destination:
                            VStack{
                                if folder.vectorData!.isEmpty {
                                    Text("no data")
                                } else {
                                    List {
                                        ForEach(folder.vectorData!) { item in
                                            Text(item.airportData.aptICAO)
                                        }
                                    }
                                }
                                //
                            }

                        ) {
                            HStack{
                            Image(systemName: "folder")
                            Text(folder.nameFolder)
                            }
                        }

                    }
                }.navigationBarTitle(Text("Folder List"), displayMode: .large)

                .navigationBarItems(trailing:
                    Button(action: {
                        self.addFolder.toggle()
                    }, label: {
                        Image(systemName: "plus")
                    })
                )
            }
        }

But app crashes when I try to list because it found the empty vector.

How can I create the optional vectorData, the user append that data after it create the folder name.

thanks


Solution

  • I assume it is here

    if folder.vectorData != nil { // << check explicitly for nil
        Text("no data")
    } else {
        List {
            ForEach(folder.vectorData!) { item in
                Text(item.airportData.aptICAO)
            }
        }
    }