【发布时间】:2018-12-23 08:50:54
【问题描述】:
我在对对象数组进行排序时遇到问题,这些对象是不同的,每个对象都有自己不同的 objectId,但是某些对象的内容是相同的,这里是我的 print(Myarray) 的输出:对象数组输入通知管理器
[notificationManager, notificationManager, notificationManager,
notificationManager, notificationManager, notificationManager,
notificationManager, notificationManager, notificationManager]
但是,我想对它们进行分组,因为在 notificationManager 中我有诸如 fromWho 和 forWho 和 Activity 之类的属性,所以有一些 notificationManagers,它们具有相同的属性 fromWho,对于相同的用户 forWho 和相同的活动,这些是为了重新组合,所以从逻辑上讲,我将需要另一个数组来添加这样的唯一值并跟踪每个元素的出现次数
myArray[i].fromWho
因为对数组本身进行排序不会有任何区别,因为从外观上看,数组已经包含不同的元素,只是对象的内容不同,这是我的代码,但肯定需要大量修改
private static func removeDuplicates( origArray: [notificationManager]) -> [notificationManager?] {
//initialize an empty array with the same count as the original array
var completionsToSend = [notificationManager?](repeating: nil, count: origArray.count)
var j = 0
for i in 0...origArray.count - 1 {
let currentElemnt = origArray[i]
if i < origArray.count - 1 {
if (currentElemnt.fromwho.username != origArray[i+1].fromwho.username && currentElemnt.forwho.username != origArray[i+1].forwho.username && currentElemnt.activity != origArray[i+1].activity) {
j += 1
completionsToSend[j] = currentElemnt;
}
}
}
if j < origArray.count - 1 {
j += 1
completionsToSend[j] = origArray[origArray.count - 1]
}
return completionsToSend
}
后来我只是调用那个函数:
let myArray = removeDuplicates(origArray: completions)
这也是我的 notificationManager 对象
class notificationManager: Hashable {
//MARK: Properties
var fromwho : PFUser!
var forwho : PFUser!
var type : Int!
var activity: ActivityModel!
var status : Int!
var date : Date!
var transaction : paymentModel!
var participant : participantModel!
var hashValue: Int {
return status.hashValue
}
init(fromwho: PFUser, forwho: PFUser, type: Int, activity: ActivityModel, status: Int, date: Date, transaction: paymentModel, participant: participantModel) {
self.fromwho = fromwho
self.forwho = forwho
self.type = type
self.activity = activity
self.status = status
self.date = date
self.transaction = transaction
self.participant = participant
}
//MARK: Inits
init(object:participantModel) {
self.fromwho = object.participant
self.forwho = object.activitymaker
self.type = object.type
self.activity = object.activity
self.status = object.status
self.date = object.updatedAt
if object.transaction != nil{
self.transaction = object.transaction
}
self.participant = object
}
任何人都可以提出任何修改建议吗?并感谢
【问题讨论】:
-
你能显示你的 notficationManager 对象吗?
-
是的,我已经修改了帖子
-
其中使用的唯一ID是什么
-
实际上我并没有为这个对象使用唯一的 id
-
数组 foreach forWho 和 fromWho 元素?