如何在Swift的方法中将字典作为参数传递? [英] How to pass dictionary as a parameter in the method in Swift?
问题描述
我在代码中创建了以下方法:
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在Services.swift类中定义 所以我想这样称呼它
SignIn is defined in Services.swift class so I am trying to call it like
obj.SignIn(objSignInDictionary)
但出现以下错误
"String!" is not identical to "String"
我错在哪里以及如何解决.几天以来,我一直被困在这里.
Where I am wrong and how it can be fixed. I am stuck here since couple of days.
推荐答案
如果您按住ALT键并单击Xcode中的objSignInDictionary
,您将看到其推断为[String : String!]
.您可以通过显式设置objSignInDictionary
类型来解决此问题:
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]
这篇关于如何在Swift的方法中将字典作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!