Enum Type 키의 사전 및 Box 유형에 의해 관리되는 값을 유지하고 싶습니다. 그러나 사전에있는 각 요소의 키/값을 함수에 전달하려고 할 때 유형 불일치에 대한 신속한 컴파일러 오류가 계속 발생합니다. Swift에서 generics 및 Any.Type을 함수와 함께 사용하는 방법
나는 상자이 선언이 :final class Box<T> {
let value: T
init(value: T) {
self.value = value
}
}
내 사전 :
let dictionary = [Car.Nissan: Box(value: NissanHandler.Type),
Car.Honda: Box(value: HondaHandler.Type),
...]
NissanHandler 및 HondaHandler가 'AutoHandler'라는 이름의 프로토콜에서 상속 두 클래스됩니다. 나는 사전에 각 키/값을 열거으로
, 나는 그것을 처리하기 위해 다른 함수에 떨어져 각 요소를 전달하고 싶습니다 :
Class A {
func processDictionary() {
for (key, values) in dictionary {
// This line produces the error,
// Cannot convert value of type 'Box<CarHandler.Type>' to expected argument type '[Product : Box<Any.Type>]'
processElement(key, value)
}
}
func processElement(key: Product, value: Box<Any.Type>) {
// Instantiate the class based on the value's class type
}
}
가 어떻게 그래서이를 유지할 수 있습니다 내 함수를 선언해야한다 런타임에 인스턴스화해야하는 클래스 매핑?