lundi 20 juin 2016

Firebase store provider photo URL (cannot convert value of type NSURL?)

I am new to Firebase and I want to store the provider photo URL however it come out the error 'Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.' I have tried different type of the method but it seems not working for example let profilePicUrl = profile.photoURL as String or let profilePicUrl = NSString(NSURL: profile.photoURL)

It is my method

func createFirebaseUser(){

        let key = ref.child("user").childByAutoId().key
        if let user = FIRAuth.auth()?.currentUser{
            for profile in user.providerData{
                let uid = profile.uid
                let providerID = profile.providerID
                let displayName = profile.displayName
                let email = ""
                let profilePicUrl = profile.photoURL


                //let userDict :[String : AnyObject] = ["name": username!"profilePicUrl": profilePicUrl!]
            let profile = Profile(uid: uid, displayName: displayName!,email: email, imageUrl: profilePicUrl)

                let childUpdates = ["/user/(key)": profile]


                ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
                    // now users exist in the database
                    print("user stored in firebase")
                })
            }
        }

and it is the data model

import Foundation

class Profile {

private var _uid: String
private var _displayName: String
private var _email: String?
private var _gender: String!
private var _city: String!
private var _imageUrl: String!

var uid: String {
    return _uid
}

var displayName: String {
    get {
        return _displayName
    }

    set {
        _displayName = newValue
    }
}

var email: String {
    get {
        return _email!
    }

    set {
        _email = newValue
    }
}

var gender: String {
    get {
        return _gender
    }

    set {
        _gender = newValue
    }
}

var city: String {
    get {
        return _city
    }

    set {
        _city = newValue
    }
}

var imageUrl: String {
    get {
        return _imageUrl
    }

    set {
        _imageUrl = newValue
    }
}

init(uid: String, displayName: String, email: String, imageUrl: String) {
    _uid = uid
    _displayName = displayName
    _email = email
    _imageUrl = imageUrl

}

Aucun commentaire:

Enregistrer un commentaire