无法将'[String:String?]'类型的值转换为预期的参数类型'[NSObject:AnyObject]?'

时间:2015-09-26 09:10:42

标签: swift parse-platform ios9 nsobject

当我使用Parse 1.8.5将数据上传到Parse时,这个云代码在“params”中有编译错误,我无法调试它。

       let params = ["phoneNumber" : userPhoneNumber, 
                    "username": username,
                    "password": userPassword,
                    "Email": userEmail
                     ]  

        PFCloud.callFunctionInBackground("sendCode", withParameters: params, block: 
              { (response: AnyObject?, error: NSError?) -> Void in
            if response?.localizedDescription != nil {
                print(error)
                var alert = UIAlertView(title: "Failure", message: "SignUp Error", delegate: self, cancelButtonTitle: "OK")
                alert.show()
            } else {
                self.activityIndicator.stopAnimating()
            }
        })

4 个答案:

答案 0 :(得分:16)

错误消息显示params的值中有可选类型。
确保所有值都已解包。

答案 1 :(得分:1)

我的项目中遇到了同样的问题。我刚刚将代码从let parameters = ["email": mail,"password":passCode ]更改为let parameters = ["email": mail as! AnyObject,"password":passCode as! AnyObject]。不确定它是否会影响其他任何事情。

答案 2 :(得分:0)

它对我有用。试试这个:

let parameters = ["number": number, 
                  "username": username,
                  "password": userPassword,
                  "Email": userEmail]
PFCloud.callFunctionInBackground("sendCode", withParameters: parameters) { results, error in
            if error != nil {  
                 print(error)   
            } else {
                self.activityIndicator.stopAnimating() 
            }
}

确保{P} Cloud中的numberusernameuserPassworduserEmailsendCode具有确切的语法

答案 3 :(得分:0)

尝试将每个元素强制转换为AnyObject

    let params = ["phoneNumber" : userPhoneNumber as! AnyObject, 
                "username": username as! AnyObject,
                "password": userPassword as! AnyObject,
                "Email": userEmail as! AnyObject
                 ]