iOS 응용 (swift)
[iOS - swift] AnyHashable과 any Hashable 개념 (associatedtype, type erase, 제네릭스)
jake-kim
2023. 6. 22. 23:44
AnyHashable
- Hashable을 wrapping하고 있는 구조체

@frozen public struct AnyHashable {
public init<H>(_ base: H) where H : Hashable
public var base: Any { get }
public static func != (lhs: AnyHashable, rhs: AnyHashable) -> Bool
}
- Hashable이 있는데 굳이 이를 wrapping한 AnyHashable을 만든 이유?
- associatedtype을 사용하는 프로토콜인, "제네릭 프로토콜"을 콜렉션 타입으로 설정하면 컴파일 에러 발생
- Hashable은 제네릭 프로토콜이므로 Hashable을 감싸서 콜렉션 타입에 유연하게 사용하기 쉽도록 하기 위해서 AnyHashable이 나온 것
제네릭 프로토콜
- 보통 프로토콜 안에 제네릭을 사용하고 싶을때 associatedtype 키워드로 사용
protocol MyProtocol {
associatedtype MyType
}
- associatedtype이 있는 프로토콜을 콜렉션의 타입으로 선언할 경우 컴파일 에러가 발생
- 이유) 콜렉션 타입의 제네릭스에 타입을 알려줄 때 구체적인 타입이 아닌 추상적인 타입을 알려 줬기 때문에 타입 추론이 어렵기 때문에 오류가 발생
- 단, associatedtype이 없는 프로토콜은 콜렉션 타입에 선언해도 에러가 발생하지 않음

- 예전까지만해도 any 키워드로 type erase를 할 수 없었으므로 AnyHashable과 같이 wrapper로 따로 생성하여 해결이 가능
protocol MyProtocol {
associatedtype MyType
}
struct AnyMyprotocol {
init<M>(_ base: M) where M: MyProtocol {
}
}
// 컴파일 에러 발생 X
var exValues = [AnyMyprotocol]()
- 이제는 any 키워드로 따로 wrapper 없이 쉽게 사용 가능
var exValues2 = [any MyProtocol]()
* 전체 코드: https://github.com/JK0369/ExAnyHasable
* 참고