How to add nil value to Swift Dictionary?

前端 未结 9 633
春和景丽
春和景丽 2021-01-31 01:20

I have made a request to my server in my app. And posted data something like this.Server side is waiting for all parameters even they are nil. But i couldn\'t add nil values to

9条回答
  •  爱一瞬间的悲伤
    2021-01-31 01:49

    var dict = [Int:Int?]()
    
    dict[0] = (Int?).none // <--- sets to value nil
    
    dict[0] = nil         // <-- removes
    
    dict[0] = .none       // <-- same as previous, but more expressive
    
    switch dict[0] {
    case .none:
        Swift.print("Value does not exist")
    
    case .some(let value):
        if let value = value {
            Swift.print("Value exists and is", value)
        } else {
            Swift.print("Value exists and is nil")
        }
    }
    

提交回复
热议问题