How to set and get data in model class?

Viewed 1565

I want to set data in model class and get the data from model class. I don't know much of MVC, but want to learn it. When I getting data that time it shows empty. how I can access the data? Please give me any solution to do this.

Below is my Model:

  class Model: NSObject {
        var RTC_minute:String!
        var RTC_hour:String!
        var RTC_day:String!
        var RTC_year:String!
        var Flag:String!

        init(RTC_minute:String, RTC_hour:String, RTC_day:String, RTC_year:String, Flag:String) {
            self.RTC_minute = RTC_minute
            self.RTC_hour = RTC_hour
            self.RTC_day = RTC_day
            self.RTC_year = RTC_year
            self.Flag = Flag
        }
    }

Below is my set data viewController:

    class viewController:UIViewControoler {
    var holidayData: Model = Model(RTC_minute:"", RTC_hour:"", RTC_day:"", RTC_year:"", Flag:"")          
    override func viewDidLoad() {
                super.viewDidLoad()
                holidayData?.RTC_minute = "0x55"
                holidayData?.RTC_hour = "0x21"
                holidayData?.RTC_day = "0x13"
                holidayData?.RTC_year = "0x20"
                print(holidayData?.RTC_minute as Any)
            }
        }

Below is my get data View Controller.

       class ViewController:UIViewController {
           var holidayData: Model = Model(RTC_minute:"", RTC_hour:"", RTC_day:"", RTC_year:"", Flag:"")
       override func viewDidLoad() {
            super.viewDidLoad()
            print(holidayData?.RTC_minute)
            print(holidayData?.RTC_hour)
            print(holidayData?.RTC_day)
            print(holidayData?.RTC_year)
        }
    }
2 Answers

You can use singleton design to assign and access your model anywhere in your module

The below code might help you

final class MainDataStore {
    static let sharedInstance = MainDataStore()

    var RTC_minute:String!
    var RTC_hour:String!
    var RTC_day:String!
    var RTC_year:String!
}

use the below methods to assign the values

MainDataStore.sharedInstance.holidayData?.RTC_minute = "0x55"

and retrieve the values

var value = MainDataStore.sharedInstance.holidayData?.RTC_minute

Note - the above code is to fulfill @vikas requirement.

Since the model contains lots of parameters you have not to initialize the model class with all properties in init(:_) method.

Change your model to this format.

class Model: NSObject {

    var RTC_minute:String!
    var RTC_hour:String!
    var RTC_day:String!
    var RTC_year:String!
    var Flag:String!

    override init() {

        self.RTC_minute = ""
        self.RTC_hour = ""
        self.RTC_day = ""
        self.RTC_year = ""
        self.Flag = ""
    }

    init(_ dict: [String: Any]?) {
        self.RTC_minute = dict?["RTC_minute"] as? String ?? ""
        self.RTC_hour = dict?["RTC_hour"] as? String ?? ""
        self.RTC_day = dict?["RTC_day"] as? String ?? ""
        self.RTC_year = dict?["RTC_year"] as? String ?? ""
        self.Flag = dict?["Flag"] as? String ?? ""
    }
}

Now you can initialize the model with

let apiResponseData = //Data from API Response and formatted with JSONSerialization
let model = Model(apiResponseData)

//For dummy model with empty values, here we no need to worry about nil values
let model = Model()

//Accessing the model
print("RTC_ minute: \(model.RTC_minute)")
Related