对成员“过滤器” swift4的含糊引用 [英] Ambiguous reference to member 'filter' swift4

查看:76
本文介绍了对成员“过滤器” swift4的含糊引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型类的列表

struct CommonRes {
var month: String?
var monthName: String?
var year: String?
var classIdCount: String?
var groupName: String?
}
 var res = [CommonRes]()

正在接收monthName的重复值。我想通过删除重复项来列出这些常见monthName的列表。因此,从我已经尝试了以下

In the response I am receiving the duplicate values of monthName. I want to make the list of these common monthName by removing duplicates. So from this I have tried the following

 self.monthList = Array(Set(res.filter({ (i : CommonRes) in res.filter({ $0.monthName == i.monthName }).count > 1 })))

但我收到错误


对成员'filter'的引用不明确

Ambiguous reference to member 'filter'


推荐答案

您可以尝试。

let monthList = Set(res.compactMap( {$0.monthName} )).sorted()

如果要保留数据顺序源,那么您可以使用 NSOrderedSet

If you want to preserve the order of your data source then you can use NSOrderedSet.

let monthListOrdered = NSOrderedSet(array: res.compactMap( {$0.monthName})).array as! [String]

但是,如果要按月份在日历上的显示顺序,则需要

However if you want the months in the order they appear on the calender, you need to sort them as dates.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM"
let monthList = Set(res.compactMap( {$0.monthName}))
let sortedMonthList = monthList.sorted(by: { dateFormatter.date(from: $0)! < dateFormatter.date(from: $1)! })

重要提示:已完成强制展开从数据源可以明显看出,您只会收到有效的月份。

Important Note: Force-unwrapping has been done because it is obvious from the data source that you will receive only valid months.

这篇关于对成员“过滤器” swift4的含糊引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆