简体   繁体   中英

Cannot convert value of type '(User) -> Dictionary<String, String>' to expected argument type 'Dictionary<String, String>'

I am still new to learning Swift. I reviewed a few other posts where it was suggested that I need to unwrap any optionals, I believe I have done so but, I am still getting the error as explained below:

Given the code snippet below

let user = User(uid: authData.uid! , email: email!, firstName: firstName!, 
        lastName: lastName!, provider: authData.provider!)

let userDictionary = User.getUserDictionary(user)

// Add new account to the Firebase database 

UserAccountService.firebaseDBService.createNewAccount(authData.uid, user: userDictionary)

Getting error

Cannot convert value of type '(User) -> Dictionary<String, String>' to

expected argument type 'Dictionary<String, String>'

on the line UserAccountService.firebaseDBService.createNewAccount(authData.uid, user: userDictionary)

Function:

func createNewAccount(uid: String, user: Dictionary<String, String>) {

    // A User is born.

    USER_REF.childByAppendingPath(uid).setValue(user)
}

User.swift:

import Foundation
import Firebase

class User: NSObject {

    let uid: String
    let email: String
    let firstName: String
    let lastName: String
    let provider: String

    // Initialize from Firebase
    init(authData: FAuthData, firstName: String, lastName: String) {
        self.uid = authData.uid!
        self.email = authData.providerData["email"] as! String
        self.firstName = firstName
        self.lastName = lastName
        self.provider = authData.provider!
    }

    // Initialize from arbitrary data
    init(uid: String, email: String, firstName: String, lastName: String, provider: String) {
        self.uid = uid
        self.email = email
        self.firstName = firstName
        self.lastName = lastName
        self.provider = ""
    }

    // Return a Dictionary<String, String> from User object
    func getUserDictionary(user: User) -> Dictionary<String, String> {
        //let provider = user.provider as String!
        let email = user.email as String!
        let firstName = user.firstName as String!
        let lastName = user.lastName as String!
        let userDictionary: [String : String] = [
            "provider" : user.provider as String!,
            "email" : email,
            "firstName" : firstName,
            "lastName" : lastName
        ]
        return userDictionary
    }
}

The User needs to be a lowercase 'u' when you call the getUserMethod as you are referring to the instance of your user class rather than the User class itself. It should look like this:

let user = User(uid: authData.uid! , email: email!, firstName: firstName!, 
    lastName: lastName!, provider: authData.provider!)

//CHANGE HERE: lowercase u on user
let userDictionary = user.getUserDictionary(user)

See if that helps.

The function you've used

User.getUserDictionary(user)

is actually the method of the class User.

So that means you need to call the method on one of the class' objects that you've instantiated instead of on the class itself.

I don't think the issue is related to optional unwrapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM