gpt4 book ai didi

methods - 如何在 Swift 的方法中将字典作为参数传递?

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:14 24 4
gpt4 key购买 nike

我在我的代码中创建了以下方法:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com