Swift中的nscountedset枚举



我正在寻找一种以更 Swift -like方式使用 NSCountedSet的方法(无论那是什么意思)。

考虑我基本上直接从Objective C翻译的以下片段。我迭代集合中的每个符号(A String),获取其相应的计数,并在字典中查找该符号的值。然后,我将该值乘以计数并将其添加到总数中。

var total = 0
for symbol in symbolSet {
    let count = symbolSet.count(for: symbol)
    let item = symbolDictionary[symbol as! String]
    let value = item?.value
    total+= (count * value!)
}

它有效,但是我有点担心Xcode建议的拆箱。因此,我正在尝试做更多的Swift喜欢的方式,以便没有所有包装的情况下更安全。

我从这样的东西开始:

symbolSet.enumerated().map { item, count in
    print(item)
    print(count)
}

,但在这里计数不是实际的计数,而是枚举索引。

我该如何前进?

您可以链接一个flatMap,然后在您的symbolSet上进行reduce操作,

  • flatMap操作应用了将symbolSet成员的尝试转换为String
  • 以下reduce操作计算symbolSet中符号的count的加权总和(对于成功转换为String实例的符号)

示例设置:

struct Item {
    let value: Int
    init(_ value: Int) { self.value = value }
}
let symbolDictionary = [
    "+" : Item(1),
    "-" : Item(2),
    "/" : Item(4),
    "*" : Item(8)
]
var symbolSet = NSCountedSet()
symbolSet.add("*") // accumulated: 8
symbolSet.add("/") // accumulated: 8 + 4 = 12
symbolSet.add("+") // accumulated: 12 + 1 = 13
symbolSet.add("-") // accumulated: 13 + 2 = 15
symbolSet.add("+") // accumulated: 15 + 1 = 16

使用链式flatMapreduce操作计算加权累加的总和(预期结果:16):

let total = symbolSet
    .flatMap { $0 as? String } /* <- attempted conversion of each symbol to 'String'         */
    .reduce(0) { $0 + symbolSet.count(for: $1) * (symbolDictionary[$1]?.value ?? 0) }
               /* |   ^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  |              |               If a key exists for the given symbol as a
                  |              |               String, extract the 'value' property of the
                  |              |               'Item' value for this key, otherwise '0'.
                  |              |
                  |   Multiply '...value' or '0' with the 'count' for the given symbol.
                  
                   Add the product to the accumulated sum of the reduce operation.           */
print(total) // 16, ok

最新更新