arraysswiftmodel-view-controllermodelnsfilemanager

How to create model in Swift?


I am a beginner in the Swift language. I have multiple arrays in one view controller from which I am loading data in the tableview.

My code is:

let filemanager = FileManager.default
var folderNameArray = [String]()
var completedExerciseArray = [String]()
var getCompletedExerciseNamesArray : [String] = []
var getCompletedDayArray = [String]()
var getCompletedDateArray = [String]()
var getCompletedTimeArray = [String]()

func getAllSavedDictionary() {
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
    for everyFileName in folderNameArray {
        if let documentPath = paths.first {
            let filePath = NSMutableString(string: documentPath).appendingPathComponent(everyFileName)
            let URL = NSURL.fileURL(withPath: filePath)
            if let dictionary = NSMutableDictionary(contentsOf: URL){
                let savedDayDateArray = dictionary.value(forKey: "DayDate") as? [String]
                let savedDayTimeArray = dictionary.value(forKey: "DayTime") as? [String]
                let savedDayNumberArray = dictionary.value(forKey: "DayNumber") as? [String]
                let savedCompletedExerciseNamesArray = dictionary.value(forKey: "completedExerciseNames") as? [String]
                self.getCompletedExerciseNamesArray.append(contentsOf: savedCompletedExerciseNamesArray!)
                self.getCompletedDayArray.append(contentsOf: savedDayNumberArray!)
                self.getCompletedDateArray.append(contentsOf: savedDayDateArray!)
                self.getCompletedTimeArray.append(contentsOf: savedDayTimeArray!)
                historyTableview.reloadData()
            }
        }
    }
}

It's working fine but I want to know how I can make a model instead of arrays and load that model into the tableview. Here in for everyFileName in folderNameArray, folderNameArray holds all the folders from FileManager.


Solution

  • This is a very open-ended question.

    A model is just a format for your data that represents it well.

    An obvious structure for a single sectioned table view is an array of structs.

    If you have a multi-sectioned table view, an array of arrays of structures works well, but ignore that unless you are dealing with a multi-section table view. (And ignore it entirely for now. No need to get too complicated at first.)

    Just create a struct for your data:

    struct CompletedWorkout: Codable {
        folderName: String
        exercise: String
        exerciseName: String
        day: String
        date: String
        time: String
    }
    

    Then an array of those structs:

    var completedWorkouts = [CompletedWorkout]()
    

    You'd then populate that array with CompletedWorkout items as needed.

    Note that I may not have captured your intended data elements correctly. Also, the String type is probably not the best choice for many of your different properties (dates, for example, are probably best stored as type Date.)

    Edit:

    To populate your model with values, you could hard code some sample data:

    completedWorkouts.append( 
        CompletedWorkout(
            folderName: "folder",
            exercise: "Crunches", //Not sure what this is supposed to be
            exerciseName: "Crunches",
            day: "Tuesday",
            date: "08/15/2023",
            time: "12:47EDT"
        }
    }
    
    completedWorkouts.append( 
        CompletedWorkout(
            folderName: "folder 2",
            exercise: "Bench press", //Not sure what this is supposed to be
            exerciseName: "Bench press",
            day: "Wednesday",
            date: "08/16/2023",
            time: "13:04EDT"
        }
    }