swift:在初始化之前通过引用传递的字典变量

khu*_*han 1 dictionary swift ios8

我正在swift初始化一个字典,然后在if条件块中使用.

    var attr:Dictionary<String,AnyObject>;

    if !isLoading {
        attr["variableSender"] = self  //Error here         
        attr["variableMode"] = "get"
        ...

    }
Run Code Online (Sandbox Code Playgroud)

我收到错误 - 变量'attr'在开始初始化之前通过引用传递.

我也试过initalizing字典

var attr:[String:AnyObject]
Run Code Online (Sandbox Code Playgroud)

1)为什么会发生这种情况以及如何解决它.我也尝试将()括号放在最后.解?

2)字典是否有明确的构造函数.

我按照本指南 https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-XID_180

airports["LHR"] = "London Heathrow"
Run Code Online (Sandbox Code Playgroud)

谢谢

Ben*_*ove 7

将您的声明更改为

var attr = [String : AnyObject]()
Run Code Online (Sandbox Code Playgroud)

这将初始化一个空的可变字典.