Search code examples
iosswiftswift4arrayobject

Sometimes Classes treat like structures in swift?


for eg:- In class DishPostedVC, I have an array of model:- var finalDatesOfDish : [DishActivationDateTimeModel]?

If i pass this variable (finalDatesOfDish) to class DishActivationVC, and do some deletion operation in class DishActivationVC, and if i go back again to class DishPostedVC, then i am getting data after deletion not the actual data, but i am just coming back not passing any data.

I don't know why but this situation occurred one more time earlier with model array, its so strange, how's it possible?

can u tell me what shld i do?

class DishActivationDateTimeModel {

var dayDate: Date? = nil
var dayDateStr: String = ""
var servingsLeft: String = ""
var firstSlotPostedDishId: String = ""
var secondSlotPostedDishId: String = ""

var startTimeDateForFirstSlot: Date? = nil
var startTimeStringForFirstSlot: String = ""
var endTimeDateForFirstSlot: Date? = nil
var endTimeStringForFirstSlot: String = ""

var startTimeDateForSecondSlot: Date? = nil
var startTimeStringForSecondSlot: String = ""
var endTimeDateForSecondSlot: Date? = nil
var endTimeStringForSecondSlot: String = ""
}

It's my model


Solution

  •   class DishActivationDateTimeModel {
    
        var dayDate: Date? = nil
        var dayDateStr: String = ""
        var servingsLeft: String = ""
        var firstSlotPostedDishId: String = ""
        var secondSlotPostedDishId: String = ""
    
        var startTimeDateForFirstSlot: Date? = nil
        var startTimeStringForFirstSlot: String = ""
        var endTimeDateForFirstSlot: Date? = nil
        var endTimeStringForFirstSlot: String = ""
    
        var startTimeDateForSecondSlot: Date? = nil
        var startTimeStringForSecondSlot: String = ""
        var endTimeDateForSecondSlot: Date? = nil
        var endTimeStringForSecondSlot: String = ""
    
        init(_ object : DishActivationDateTimeModel) {
            self.dayDate = object.dayDate
            self.dayDateStr = object.dayDateStr
    //        ....
    //        ....
            self.endTimeStringForSecondSlot = object.endTimeStringForSecondSlot
        }
    }
    extension Array where Element : DishActivationDateTimeModel {
    
        func copyModelArray() -> [DishActivationDateTimeModel] {
            var array : [DishActivationDateTimeModel] = []
            for object in self {
                array.append(DishActivationDateTimeModel(object))
            }
            return array
        }
    }
    

    If you don't want to make changes in your main data then copy your model array using copyModelArray method. And use your operational model array. So, its not affect to your main model data array.