我从我的Firebase数据库(JSON数据库)中检索数字值,然后将此数字显示为textField
,尽管我在尝试显示它。iOS Swift:无法将值类型'__NSCFNumber'转换为'NSString'
未能投值类型“__NSCFNumber”到“的NSString”
我怎样才能正确地检索到的值转换为字符串,考虑到这个值可能是字符串和数字之间变化当我检索它。
这里是我的代码:
let quantity = child.childSnapshot(forPath: "quantity").value // Get value from Firebase
// Check if the quantity exists, then add to object as string.
if (!(quantity is NSNull) && ((quantity as! String) != "")) {
newDetail.setQuantity(quantity: quantity as! String)
}
我们不能使用'.description'吗? –
第二个选项有效,但不是当数量值是以字符串值开始时。是否有一个'else if'语句可以用来检查值是否是一个String? – n00bAppDev
太好了,谢谢!这个答案和上面的答案都是一样的,我选择哪一个作为答案? – n00bAppDev