mavericklab mavericklab - 1 year ago 56
iOS Question

swift 3 Type 'Any' error

So I had a perfectly working project using swift 2. Then I updated xcode and it converted the code to Swift 3. Now im getting this error every time I call snapshot. I'm using Firebase as my backend. This is my code.

import Foundation
import Firebase
import FirebaseDatabase
import FirebaseAuth


struct User {

var username: String!
var email: String!
var photoUrl: String!
var country: String!
var ref: FIRDatabaseReference?
var key: String!


init(snapshot: FIRDataSnapshot){

key = snapshot.key
username = snapshot.value!["username"] as! String
email = snapshot.value!["email"] as! String
photoUrl = snapshot.value!["photoUrl"] as! String
country = snapshot.value!["country"] as! String
ref = snapshot.ref

}




}


I'm getting the error that reads: Type 'Any' has no subscript members. This error is on the lines that have snapshot.value in them. Does anyone have any idea on how to fix this?

Answer Source

What about something like:

let values = snapshot.value as! Dictionary<String,String>
username = values["username"]
...