简体   繁体   English

Swift字典作为属性不初始化

[英]Swift dictionary as property does not init

I have a class which has a swift dictionary as internal variable. 我有一个带有快速字典作为内部变量的类。

   class OAuth: NSObject {

    public var oauthParameters:[String:String]
    public var SignatureSecret:String

    public var accessToken:String? = nil
    public var tokenSecret: String? = nil

    public init(consumerKey:String, consumerSecret:String, accessToken:String?, tokensecret:String?){

        self.oauthParameters = ["oauth_signature_method" : "SHA1"]

        if accessToken{
            self.oauthParameters.updateValue(accessToken!, forKey: "oauth_token")
        }

        if tokensecret{
            self.SignatureSecret = "\(consumerSecret)&\(tokensecret!)"
        }
        else {
            self.SignatureSecret = "\(consumerSecret)&"
        }

    }
}

I would like to assign some keys of the dictionary in function of the passed arguments, but the problem is that the variable oauthParameters does not retain the values passed as literals :( 我想在传递的参数的函数中分配字典的某些键,但是问题是变量oauthParameters不保留作为文字传递的值:(

EDIT: I guessed the dictionary was not initialzed because LLDB printed me out an empty dictionary (Xcode6B4) wehereas a println confirms all were ok. 编辑:我猜字典没有初始化,因为LLDB将我打印出一个空字典(Xcode6B4),因此println确认一切正常。

It looks like your initializer isn't complete. 看来您的初始化程序尚未完成。 For some reason the Playground isn't giving me an actual error, but you need to give self.SignatureSecret a value before exiting init() : 由于某些原因,Playground并没有给我一个实际的错误,但是您需要在退出init()之前给self.SignatureSecret一个值:

public init(consumerKey:String, consumerSecret:String, accessToken:String?, tokensecret:String?)
{
    self.oauthParameters = ["oauth_signature_method" : "SHA1"]
    self.SignatureSecret = String()
}

Once that's fixed I can see the values in self.oauthParameters : 解决之后,我可以在self.oauthParameters看到值:

println(o.oauthParameters["oauth_signature_method"]!)
// prints "SHA1"

It loos like you need to initialize oauthParameters 好像您需要初始化oauthParameters
try something like this: 尝试这样的事情:

public var oauthParameters: [String: String] = [String: String]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM