0

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.

2 Answers 2

0

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" } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. Can you tell me that, How can i append the model with data in my code?
In a real-world application, you would probably load your data from a file or database or fetch it from a server.
Yes here i am loading data from filemanager. Can you please tell me that How I can append data in the model? Like i am appending data in array in my code above.
0

No need to be confused with Codable. its just like you are mapping data into models

class UserImage: Codable { let id: Int let objectURL: String let isVideo: Bool let priorityCounter: Int let isDeleted: Bool let user: Int let videoAvailable : Bool let videoScreenshotUrl: String enum CodingKeys: String, CodingKey { case id case objectURL = "object_url" case isVideo = "is_video" case priorityCounter = "priority_counter" case isDeleted = "is_deleted" case user case videoScreenshotUrl = "video_screenshot_url" case videoAvailable = "video_available" } init(id: Int, objectURL: String, isVideo: Bool, priorityCounter: Int, isDeleted: Bool, user: Int, videoScreenshotUrl: String, videoAvailable: Bool ) { self.id = id self.objectURL = objectURL self.isVideo = isVideo self.priorityCounter = priorityCounter self.isDeleted = isDeleted self.user = user self.videoScreenshotUrl = videoScreenshotUrl self.videoAvailable = videoAvailable } } 

you can do this by:

let dataObj = dataString.data(using: .utf8)! do { guard let jsonObject = try JSONSerialization.jsonObject(with: dataObj, options : .allowFragments) as? Dictionary<String,Any> else { return } guard let user = jsonObject[APIKey.user] as? Dictionary<String,Any> else { return } guard let image = user[APIKey.image] as? Dictionary<String,Any> else { return } } catch let error as NSError { print(error) } 

5 Comments

Thanks for your answer. How can i append the model?
in swift or objective?
Hi, here i'm not using api call for fetching the data. I'm using local database.
You would be better off to throw away your current file format and use the Codable protocol to serialize and deserialize your data. Google Codable There are tons of examples online.
please have a look at this stackoverflow.com/questions/77137171/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.