我在 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)")
你應該添加:
import SwiftUI
到您的腳本,因為它包含remove(atOffsets:)
實現,而 XCode 不會隱式導入此框架。
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras