Assign value to optional dictionary in Swift

后端 未结 4 1189
北恋
北恋 2020-12-10 04:42

I\'m finding some surprising behavior with optional dictionaries in Swift.

var foo:Dictionary?

if (foo == nil) {
    foo = [\"bar\": \         


        
4条回答
  •  甜味超标
    2020-12-10 05:42

    I tried this for Swift 3.1 and it worked:

    if (myDict?[key] = value) == nil {
        myDict = [key: value]
    }
    

提交回复
热议问题