Home > OS >  Appending New Element to Array Swift
Appending New Element to Array Swift

Time:03-11

I am trying to filter notifications based on date. So I will show them in each separate sections on tableview.

So far I was able to get date and compare dates with today, last month and older as below.

var todaySectionNotificationsArray = [NewNotificationModel]()
var lastMonthSectionNotificationsArray = [NewNotificationModel]()
var oldSectionNotificationsArray = [NewNotificationModel]()
let now = Date()
let today = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
let lastMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
var old = Calendar.current.date(byAdding: .month, value: -2, to: Date())!

I created 3 empty arrays for them and tried to append element if their date is what I am looking for.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch cellTypes[indexPath.row] {
    case .notification:
        if (notificationsViewModel[indexPath.row].toDate()! < today) {
            todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row])
        } else if (notificationsViewModel[indexPath.row].toDate()! < lastMonth) {
            lastMonthSectionNotificationsArray.append(contentsOf:notificationsViewModel[indexPath.row])
        } else if (notificationsViewModel[indexPath.row].toDate()! > lastMonth) {
            oldSectionNotificationsArray  = notificationsViewModel[indexPath.row])
        }
        return configureNotificationCell(indexPath: indexPath)
    case .dummy: return configureDummyCell(indexPath: indexPath)
    case .empty: return configureEmptyCell(indexPath: indexPath)
    case .error: return configureErrorCell(indexPath: indexPath)
    }
}

But those .append are giving me errors "No exact matches in call to instance method 'append'" and = operator gives the error of "Operator function ' =' requires that 'NewNotificationViewModel' conform to 'Sequence'"

This is my NewNotificationViewModel

final class NewNotificationViewModel {

// MARK: Properties
var notificationModel: NewNotificationModel!

private(set) var id: String?
private(set) var fullName: String!
private(set) var firstLetterName: String!
private(set) var profileImageString: String?
private(set) var profileImageURL: URL?
private(set) var date: String?
private(set) var isRead: Bool!
private(set) var isActionButtonActive: Bool?
private(set) var groupName: String?
private(set) var type: Int?
private(set) var itemDescription: String?
private(set) var itemId: String?
private(set) var mail: String?
private(set) var userId: String!
private(set) var dateTimeStamp: Double!
private(set) var interactionCount: String?

// MARK: Initialization
init(notificationModel: NewNotificationModel) {
    self.notificationModel = notificationModel
    self.id = notificationModel.id
    self.fullName = getFullName()
    self.firstLetterName = getFirstLetters()
    self.profileImageURL = getProfileImageURL()
    self.date = getDate()
    self.isRead = notificationModel.isRead
    self.isActionButtonActive = notificationModel.isActionButtonActive
    self.groupName = notificationModel.groupName
    self.type = notificationModel.type
    self.itemDescription = notificationModel.item?.description
    self.itemId = notificationModel.item?.itemId
    self.mail = getMail()
    self.userId = getUserId()
    self.dateTimeStamp = getTimeStamp()
    self.interactionCount = notificationModel.interactionCount
    }
  }
extension NewNotificationViewModel {
    
    func toDate() -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        dateFormatter.timeZone = TimeZone.current
        return dateFormatter.date(from: self)
    }

and this is NewNotificationModel

  class NewNotificationModel: Serializable {
    var id: String?
    var fromUser: NewNotificationFromUserModel!
    var type: Int?
    var item: NewNotificationItemModel?
    var isRead: Bool?
    var creationTime: String?
    var isActionButtonActive: Bool?
    var groupName: String?
    var interactionCount: String?
    }

So now I do not know what to do. Even coming up with this solution took 3-4 hours of mine.

CodePudding user response:

Looks like you're accidentally passing in self instead of the date property on the NewNotificationViewModel toDate() function

func toDate() -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    dateFormatter.timeZone = TimeZone.current
    return dateFormatter.date(from: self)
}

This line: return dateFormatter.date(from: self) should be return dateFormatter.date(from: date ?? "")

CodePudding user response:

Based on your model and viewModel i think you can append the property(notificationModel) of viewModel to the array like below

todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row].notificationModel)
  • Related