I know this is a duplicate of Cast from FIRRemoteConfigValue to unrelated type String always fails . But the accepted answer there is that it's just a bug .
Which i don't think it is.
According to another solution, I have to force-cast the datatype every time I retrieve or instantiate a variable. I was hoping to find a different and elegant solution to this.
Everything was working fine, but after I installed Pod 'FirebaseRemoteConfig'
, I am having this warning on every line I instantiate a variable with a Snapshot value that I receive.
Cast from FIRRemoteConfigValue to unrelated type String always fails
When I run my app, it crashes there.
Bugged Code
FIRDatabase.database().reference().child("Posts").observe(.value, with: {(recievedSnap) in
if recievedSnap.exists(){
if let dict = recievedSnap.value as? [String:AnyObject]{
for each in dict{
let str = each.value["text"] as! String //Line of warning(and crash when the app is run)
}
}
}
})
Working Code
FIRDatabase.database().reference().child("Posts").observe(.value, with: {(recievedSnap) in
if recievedSnap.exists(){
if let dict = recievedSnap.value as? [String:AnyObject]{
for each in dict{
if let eachValue = each.value as? [String:AnyObject]{
let str = eachValue["text"] as! String
}
}
}
}
})
I am looking for explanation as to:
What changed after Pod 'FirebaseRemoteConfig'
installation that it started giving me this error?
Why am I required to force cast every time I initialize a variable?
Is there a better approach?
Finally got the solution...
Use valueForKey
Instead of []
bracket access value, because FIRRemoteConfigValue
dont have support to access value using []
Then all value goes away...
For Example
Warning code...
Below code give a warning...
let id = snapshot.value!["senderId"] as! String
let text = snapshot.value!["text"] as! String
let locationId = snapshot.value!["location"] as! String
Use Like this
let id = snapshot.value.valueForKey("senderId") as! String
let text = snapshot.value.valueForKey("text") as! String
let locationId = snapshot.value.valueForKey("location") as! String
Warming is not appear longer.....
NOTE: If you use like above then old code also working fine...
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.