I guess that I capture a bug in methods lookup or an intended behavior. Don't really now.
Consider following example:
// MARK: StorageFile.swift
// MARK: Protocols
protocol StorageHolderProtocol {
associatedtype Key: Hashable
associatedtype Value
subscript (_ key: Key) -> Value? { get set }
}
protocol StorageHolderKeyStringAndValueAnyProtocol: StorageHolderProtocol where Self.Key == String, Self.Value == Any {
func add<T>(_ entry: T)
func get<T>(by type: T.Type) -> T?
}
private protocol StorageHolderProtocol_Private: StorageHolderProtocol {
var store: Dictionary<Key, Value> { get set }
}
extension StorageHolderProtocol_Private {
subscript (_ key: Key) -> Value? {
get {
self.store[key]
}
mutating set {
self.store[key] = newValue
}
}
}
private protocol StorageHolderKeyStringAndValueAnyProtocol_Private: StorageHolderKeyStringAndValueAnyProtocol, StorageHolderProtocol_Private {
}
extension StorageHolderKeyStringAndValueAnyProtocol_Private {
func typeName(some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(type(of: some))"
}
func add<T>(_ entry: T) {
let key = typeName(some: T.self)
// self.store[key] = entry
}
func get<T>(by type: T.Type) -> T? {
let key = typeName(some: T.self)
return self.store[key] as? T
}
}
// MARK: Class
class MyStore: StorageHolderKeyStringAndValueAnyProtocol, StorageHolderKeyStringAndValueAnyProtocol_Private {
var store: Dictionary<String, Any> = .init()
}
// MARK: ExampleFile.swift
class Example {
static func example() {
let store = MyStore()
store. // <- doesn't show methods `.get` and `.add` in pop-up.
store.add(SomeClass.self) // <- works.
}
}
If class Example
is defined in StorageFile.swift
near MyStore
, methods lookup works without hassle.