How to pass dictionary as a parameter in the method in Swift?

杀马特。学长 韩版系。学妹 提交于 2020-01-01 09:36:09

问题


I have created following method in my code:

func SignIn(objDictionary:Dictionary<String,String>)
{
    //Body of method
}

I need to pass the following dictionary as a parameter in this method which is defined below:

let objSignInDictionary:Dictionary = ["email":emailTextField.text, "password":passwordTextField.text]

How can I pass this dictionary in the above method as a parameter?

This should be noted that method is in another class and I am calling the method by creating its object as follows:

let obj = Services()

SignIn is defined in Services.swift class so I am trying to call it like

obj.SignIn(objSignInDictionary) 

but getting following error

"String!" is not identical to "String"

Where I am wrong and how it can be fixed. I am stuck here since couple of days.


回答1:


If you alt+click objSignInDictionary in Xcode you will see its inferred as [String : String!]. You can fix this by explicitly setting objSignInDictionary type:

let objSignInDictionary:[String:String] = ["email":emailTextField.text, "password":passwordTextField.text]


来源:https://stackoverflow.com/questions/26096178/how-to-pass-dictionary-as-a-parameter-in-the-method-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!