作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在我的代码中创建了以下方法:
func SignIn(objDictionary:Dictionary<String,String>)
{
//Body of method
}
我需要在下面定义的这个方法中将以下字典作为参数传递:
let objSignInDictionary:Dictionary = ["email":emailTextField.text, "password":passwordTextField.text]
如何在上述方法中将此字典作为参数传递?
需要注意的是,该方法在另一个类中,我通过创建其对象来调用该方法,如下所示:
let obj = Services()
SignIn 在 Services.swift 类中定义所以我试着这样调用它
obj.SignIn(objSignInDictionary)
但出现以下错误
"String!" is not identical to "String"
我错在哪里以及如何解决。我被困在这里几天了。
最佳答案
如果您在 Xcode 中按住 alt 并单击 objSignInDictionary
,您将看到它被推断为 [String : String!]
。您可以通过显式设置 objSignInDictionary
类型来解决此问题:
let objSignInDictionary:[String:String] = ["email":emailTextField.text, "password":passwordTextField.text]
关于methods - 如何在 Swift 的方法中将字典作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096178/
我是一名优秀的程序员,十分优秀!