我有一个带有任务列表字段的条目数组(如下所示),字段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
如何遍历Sequence &获取数据的出现次数
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
Array &想要找到它的两个属性(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键的事件字典。