【问题标题】:issue sorting an array of objects, different objects but same content问题排序对象数组,不同的对象但相同的内容
【发布时间】: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 元素?

标签: ios arrays swift sorting


【解决方案1】:

好的,所以根据forWhofromWhoactivity 属性过滤没有重复的新数组。

我创建了一个简单的模型来模拟您的问题,因为实现完全相同的情况有点困难,但是让我们深入研究一下。

struct Item {  // as your Notification object
    var id: Int
    var name: String
    var nick: String
    var activity: Int
}
extension Item: Equatable {
    static func == (lhs: Item, rhs: Item) -> Bool {
     let name = lhs.name == rhs.name
     let activity = lhs.activity == rhs.activity
     let nick = lhs.nick == rhs.nick
     return name && activity && nick
    }
}
// sample  array of objects
let items: [Item] =  [
    Item(id: 1, name: "foo", nick: "foo", activity: 0),
 Item(id: 2, name: "foo", nick: "foo", activity: 0),
 Item(id: 3, name: "bee", nick: "bee", activity: 1),
 Item(id: 4, name: "boo", nick: "boo", activity: 2),
 Item(id: 5, name: "bee", nick: "bee", activity: 1),
 Item(id: 6, name: "cee", nick: "cee", activity: 3),
]

// filtration.
var newItems: [Item] = []
for item in items {
    if !newItems.contains(item){
        newItems.append(item)
    } else {
       print("alreadyExisted")
    }
}

如您所见,我们只是确认Equatable 协议,该协议允许我们比较自定义对象或== 运算符之间的几乎任何东西。

在上面你可以看到我使用它来返回它的 Bool 值,基于每个 lhs 上的 namenickactivity 是否彼此相等。

我可以简单地询问我的array 是否在上面的filtration 部分中包含item,因为它会根据这些属性检查该项目是否已经存在。

现在

对于您的情况,请务必确认上述Equatable 协议并执行您的检查。

由于您的基础属性并不简单StringsInts 因此,您可能也必须向这些模型确认Equatable,这应该可以让您大致了解它是如何完成的。

在 Playground 上测试代码,

输出

2 项已存在,

new Array [Item(id: 1, name: "foo", nick: "foo", activity: 0),

Item(id: 3, name: "bee", nick: "bee", activity: 1),

Item(id: 4, name: "boo", nick: "boo", activity: 2),

项目(id:6,名称:“cee”,昵称:“cee”,活动:3)]

【讨论】:

  • 它成功了,我只是改变了一件事,我只使用 fromWho 和 forWho 进行了比较,似乎它也没有比较活动,所以我不得不从公平扩展中删除它
猜你喜欢
  • 2016-09-26
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多