I am trying to fix an issue with a firebase snapshot that is returning a blank value due to expecting a string and it's an integer value, so it returns "".
Original code here:
let myTrString = "Translation/" + myTranslation + "/codebase"
let myRef2 = dbRef.reference().child(myTrString)
myRef2.child(String(pageIndex)).observeSingleEvent(of: .value, with: { [self] (snapshot) in
let value = snapshot.value as? NSDictionary
print(value)
let oText = value?["text"] as? String ?? 0
let oCodebase = value?["code"] as? String ?? ""
if oText == 0 && pageIndex > 1 {
self.navigationController?.navigationItem.hidesBackButton = false
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.isToolbarHidden = false
self.navigationController?.navigationItem.hidesBackButton = false
self.navigationController?.setToolbarHidden(false, animated: true)
self.navigationController?.popViewController(animated: true)
} else {
self.lblTranslated.text = oText
}
}) { (error) in
print(error.localizedDescription)
}
When I run this, the oText is basically an empty string; probably due to it being an Int64 type. Unfortunately that is what the data is, we're not in a position to change the json data type.
So I figured I would change the the following
let oText = value?["text"] as? String ?? 0
to
let oText = value?["text"] as? Int ?? 0
But with that I get:
Compiler is unable to type-check this express in reasonable time
This should stay as a single observable event since we are translating one sentence.
I tried to use a predefined struct but that didn't work either.
[Update]
myRef2.child(String(pageIndex)).observeSingleEvent(of: .value, with: { [self] (snapshot) in
if let value = snapshot.value as? [String : Any] {
let oText = value["text"]
let oCodebase = value["code"]
if Int(oText) == 0 && pageIndex > 1 {
...
}
}
I'm now getting an error on the Int(oText) == 0 line with the following:
No exact matches call to initializer
If I take out the Int(oText) and just make it oText I get the following error which I agree with :
Cannot convert value of type 'Any?' to expected argument type 'Int'
There has to be a clean way to cast this value. I have seen all kinds of bastardized and hacked up methods to cast, but I find it hard to believe that Apple made this without an ability to cast an ANY to an Int.