您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

iOS Swift:无法将值类型'__NSCFNumber'强制转换为'NSString'

iOS Swift:无法将值类型'__NSCFNumber'强制转换为'NSString'

错误是说您的数量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。

Swift 2022/1/1 18:14:24 有814人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶