【发布时间】:2021-12-22 03:01:16
【问题描述】:
我在 Playground 中有这个非常简单的代码。它编译时没有错误,但是当我尝试运行它时,remove(atOffsets:) 函数出现错误。这是为什么呢?
enum CandyColor: CaseIterable {
case red, green, yellow, brown
}
enum CandyFilling: CaseIterable {
case chocolade, nuts
}
extension CaseIterable {
static var random: Self {
Self.allCases.randomElement()!
}
}
typealias Candy = (color: CandyColor, filling: CandyFilling)
var candies = (0...20).map{ _ in Candy(color: CandyColor.random, filling: CandyFilling.random)}
var redWithChocolade = [Candy]()
var yellowWithNuts = [Candy]()
var brownAndGreenWithChocolade = [Candy]()
var indiciesToRemove = IndexSet()
for (index, candy) in candies.enumerated() {
switch candy {
case (.red, .chocolade):
redWithChocolade.append(candy)
indiciesToRemove.insert(index)
case (.yellow, .nuts):
yellowWithNuts.append(candy)
indiciesToRemove.insert(index)
case let(c) where [.brown, .green].contains(c.color) && c.filling == .chocolade:
brownAndGreenWithChocolade.append(c)
indiciesToRemove.insert(index)
default:
break
}
}
candies.remove(atOffsets: indiciesToRemove) // Error: candidate expects value of type 'Int' for parameter #1
print("Left in heap: \(candies)")
【问题讨论】:
-
在 XCode 12.5 上运行良好,你有什么版本的 XCode?span>
-
我正在使用 Xcode 13.0 (13A233),@iUrii。