例如:
在类DishPostedVC中,我有一个模型数组:-
var finalDatesOfDish:[DishActivationDateTimeModel]?
如果我将此变量(finalDatesOfDish)传递给DishActivationVC类,并在DishActivationVC类中进行一些删除操作,并且如果我再次返回DishPostedVC类,则删除后我将获得数据,而不是实际数据,但我只是不返回传递任何数据。
我不知道为什么,但是这种情况在模型数组中又发生了一次,这太奇怪了,怎么可能?
你能告诉我我该怎么做吗?
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 = ""
}
是我的模特
最佳答案
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
}
}
如果您不想在主数据中进行更改,请复制您的
使用
copyModelArray
方法的模型数组。并使用您的运营模型数组。因此,它不会影响您的主模型数据数组。
关于ios - 有时,类很快就会像结构一样对待?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52037147/