Home > Mobile >  How can I get the value in the load function to not be an optional?
How can I get the value in the load function to not be an optional?

Time:03-21

Im trying to get the value given in the Load() function to unwrap so it isnt an optional, but it wont unwrap any further and its still returning an optional.

let t = true; let f = false
var saved: [Int: Any?] = [0: nil]

func prog() {
    
    Save(Key: 1, Value: 7)
    Save(Key: 2, Value: Condition(Save: 1, Value: 9, Return: f))
    Display(Save: 2)
    Display(Save: 3)
    DisplayValue(Value: Load(Save: 2))
    
}; prog()





func Save(Key: Int?, Value: Any) {
    var _Key = Key ?? 0
    saved[_Key] = Value
}

public func Load(Save: Int?) -> Any {
    var _Key = Save ?? 0
    return saved[_Key]!
}

func Display(Save: Int?) {
    var _Key = Save ?? 0
    if saved[_Key] == nil {
        print("nil")
    } else {
        print(saved[_Key]!!)
    }
}

func DisplayValue(Value: Any?) {
    print(Value! ?? "nil")
}

func Condition(Save: Int?, Value: Any?, Return: Any) -> Any {
    var _Key = Save ?? 0
    if saved[_Key] is String && Value is String {
        if saved[_Key] as! String == Value as! String {
            return Return ?? ""
        }
    }
    else if saved[_Key] is Float && Value is Float {
        if saved[_Key] as! Float == Value as! Float {
            return Return ?? ""
        }
    }
    else if saved[_Key] is Double && Value is Double {
        if saved[_Key] as! Double == Value as! Double {
            return Return ?? ""
        }
    }
    else if saved[_Key] is Int && Value is Int {
        if saved[_Key] as! Int == Value as! Int {
            return Return ?? ""
        }
    }
    else if saved[_Key] is Bool && Value is Bool {
        if saved[_Key] as! Bool == Value as! Bool {
            return Return ?? ""
        }
    }
    else if saved[_Key] == nil || Value == nil {
        return ""
    }
    if Return is Bool {
        if Return as? Bool == false {
            return true
        } else {
            return false
        }
    }
    return ""
}

Ive gone through the entirety of the code. No function is returning optionals or saving optionals to the saved dictionary. Can someone explain where the optional is occurring, and/or how to prevent it?

CodePudding user response:

var saved: [Int: Any?] = [0: nil]

This says that the returned values of this dictionary are Any?. So subscripting saved[key] will return an Any?? (i.e. an Optional<Optional<Any>>), since that Any? might or might not exist, since the key might not be in the dictionary. Any?? is a type that nightmares are made of. Never allow Any?? to exist. Do not create Any? on purpose. And strongly avoid Any.

(The reason that Optional<Any> is such a mess of a type is that Optional is itself of type Any, since everything is of type Any. And Swift will automatically promote things to Optionals if it thinks it's needed. So Any is can be promoted to Any?, but Any? is a subtype of Any. This loop, while pretty deterministic, leads to all kinds of confusion.)

Replace saved with a proper type that doesn't rely on Any, and this problem will go away.

Following your style, the last line of Load needs two ! to do what you expect (like your Display uses):

return saved[_Key]!!

But this is deeply, deeply broken. You need to redesign your types, likely using an enum to replace Any.

  • Related