have code that resembles this
I created a custom subscript to do the unwrapping for me to make things easier.
I know that there is a safer way to do this by creating a safe subscript that returns a default value instead of force unwrapping
enum MyEnum {
case one
case two
case three
}
struct MyStruct {
static var empty: Self {
return .init()
}
//Some Variables Here
}
class MyClass {
var index = 0
var data: [MyEnum: MyStruct] = [
.two: .empty,
.three: .empty,
.one: .empty
]
var allTypes: [MyEnum] {
switch Bool.random() {
case true:
return [.two, .three]
case false:
return [.one]
}
}
var currentSelectedType: MyEnum {
return allTypes[index]
}
func myMethod(type: MyEnum) {
let one: MyStruct = data[type]
let two: MyStruct = data[currentSelectedType]
let three: MyStruct = data[allTypes[index]]
let four: MyStruct = data[.one]
}
}
private extension Dictionary {
subscript(_ key: Key) -> Value where Key == MyEnum, Value == MyStruct {
get {
return self[key]!
}
set {
self[key] = newValue
}
}
}
Why the compiler does not recognize my subscript in the first two lines in myMethod?