我有一个带有任务列表字段的条目数组(如下面所示),字段important和urgent是Strings,过去我有四个状态组合(重要与紧急、重要与无、无&紧急以及无&无)。因此,我想数一数,我有多少个组合,在总数。
entry = [
TaskList(
id: isPlus,
isComplete: displayComplete,
dateAdded: displayAdded!,
name: displayName!,
secondaryCategory: displayCategory!,
important: displayImportance!, //String
urgent: displayUrgency! //String
),
TaskList(...)
]即:
important & urgent = 3,important & none = 2,
none & urgent = 0,none & none = 4
发布于 2021-10-23 06:04:38
will如何在序列上进行迭代&获得数据的出现。
var greetingArr = Array("Hello, playground")
var occurrences = Dictionary(greetingArr.map{($0, 1)}) { i, j in i + 1 }
//or Dictionary(greetingArr.map{($0, 1)}) { $0 + $1 }
//or Dictionary(greetingArr.map{($0, 1)}, uniquingKeysWith: +)
// occurrences ⬻ ["e": 1, "l": 3, "o": 2, "n": 1, "a": 1, ",": 1, "y": 1
// " ": 1, "d": 1, "H": 1, "u": 1, "p": 1, "r": 1, "g": 1]在本例中,迭代器是您的entry数组&希望找到它的两个属性(important & urgent)的特定组合。如果这些属性是字符串类型:
var dict = Dictionary(entry.map{($0.important + "-" + $0.urgent, 1)}) { $0 + $1 }如果有一个函数给出Int,从这两个属性。
important,urgent ⤖ 3,important,none ⤖ 2,none,urgent ⤖ 0&none,none ⤖ 4
然后您可以映射entry.map{($0.intFromTwoProperties, 1)}而不是以前的映射,
但是这会给你一个带有Int键的事件字典。
https://stackoverflow.com/questions/69685115
复制相似问题