错误是说您的数量是Number
,您不能直接将数字转换为String
,尝试这样。
newDetail.setQuantity(quantity: "\(quantity)")
要么
if let quantity = child.childSnapshot(forPath: "quantity").value as? NSNumber {
newDetail.setQuantity(quantity: quantity.stringValue)
}
else if let quantity = child.childSnapshot(forPath: "quantity").value as? String {
newDetail.setQuantity(quantity: quantity)
}
或使用单if语句
if let quantity = child.childSnapshot(forPath: "quantity").value,
(num is NSNumber || num is String) {
newDetail.setQuantity(quantity: "\(quantity))
}
使用第二和第三个选项,无需检查nil。