将新元素追加到数组 Swift
Appending New Element to Array Swift
我正在尝试根据日期过滤通知。所以我将在 tableview 的每个单独部分中显示它们。
到目前为止,我能够获取日期并将日期与今天、上个月和更早的日期进行比较,如下所示。
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())!
我为他们创建了 3 个空数组,如果他们的日期是我要查找的日期,我会尝试附加元素。
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)
}
}
但是那些 .append 给我错误
“在调用实例方法时没有完全匹配 'append'” 和 += 运算符给出错误 ”运算符函数 '+=' 要求 'NewNotificationViewModel' 符合 'Sequence'"
这是我的 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)
}
这是 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?
}
所以现在我不知道该怎么办。即使想出这个解决方案也花了我 3-4 个小时。
看起来你在 NewNotificationViewModel
toDate() 函数
上不小心传入了 self
而不是 date
属性
func toDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: self)
}
这一行:return dateFormatter.date(from: self)
应该是 return dateFormatter.date(from: date ?? "")
根据您的模型和 viewModel,我认为您可以将 viewModel 的 属性(notificationModel) 附加到数组,如下所示
todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row].notificationModel)
我正在尝试根据日期过滤通知。所以我将在 tableview 的每个单独部分中显示它们。
到目前为止,我能够获取日期并将日期与今天、上个月和更早的日期进行比较,如下所示。
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())!
我为他们创建了 3 个空数组,如果他们的日期是我要查找的日期,我会尝试附加元素。
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)
}
}
但是那些 .append 给我错误 “在调用实例方法时没有完全匹配 'append'” 和 += 运算符给出错误 ”运算符函数 '+=' 要求 'NewNotificationViewModel' 符合 'Sequence'"
这是我的 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)
}
这是 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?
}
所以现在我不知道该怎么办。即使想出这个解决方案也花了我 3-4 个小时。
看起来你在 NewNotificationViewModel
toDate() 函数
self
而不是 date
属性
func toDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: self)
}
这一行:return dateFormatter.date(from: self)
应该是 return dateFormatter.date(from: date ?? "")
根据您的模型和 viewModel,我认为您可以将 viewModel 的 属性(notificationModel) 附加到数组,如下所示
todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row].notificationModel)