iOS Swift:无法将值类型“__NSCFNumber”转换为“NSString”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40798220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:50:53  来源:igfitidea点击:

iOS Swift: Could not cast value type '__NSCFNumber' to 'NSString'

iosswiftfirebase

提问by n00bAppDev

I am retrieving a number value from my Firebase database (JSON db) and then displaying this number into a textField, although I get this error when I try to display it.

我正在从我的 Firebase 数据库(JSON db)中检索一个数字值,然后将该数字显示到 a 中textField,尽管我在尝试显示它时遇到了这个错误。

Could not cast value type '__NSCFNumber' to 'NSString'

无法将值类型“__NSCFNumber”转换为“NSString”

How can I properly convert the retrieved value to a String, taking into consideration that this value maybe change between a String and a Number when I retrieve it.

考虑到该值在检索时可能在字符串和数字之间发生变化,我如何正确地将检索到的值转换为字符串。

Here is my code:

这是我的代码:

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)
}

回答by Nirav D

The error is saying that your quantity is Numberand you cannot directly convert number to String, try like this.

错误是说你的数量是Number,你不能直接将数字转换为String,试试这样。

newDetail.setQuantity(quantity: "\(quantity)")

Or

或者

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)
} 

Or With Single if statement

或使用单个 if 语句

if let quantity = child.childSnapshot(forPath: "quantity").value,
   (num is NSNumber || num is String) {
     newDetail.setQuantity(quantity: "\(quantity))
}

Using second and third option there is no need to check for nil.

使用第二个和第三个选项不需要检查 nil。

回答by A.G

Swift 4:

斯威夫特 4:

let rollNumber:String = String(format: "%@", rollNumberWhichIsANumber as! CVarArg)

回答by Devang Tandel

You may convert your NSNumber value to string like this too, its more traditional and basic approach to format a string

您也可以将 NSNumber 值转换为这样的字符串,这是格式化字符串的更传统和基本的方法

 newDetail.setQuantity(String(format: "%@", quantity))

回答by Joyal Clifford

Swift 4.

斯威夫特 4.

 newDetail.setQuantity(String(describing: rollNumberWhichIsANumber))

回答by iOS

In swift 4.1 and Xcode 9.4.1

在 swift 4.1 和 Xcode 9.4.1 中

newDetail.setQuantity(quantity: "(quantity)")

newDetail.setQuantity(数量:“(数量)”)

We can convert to string like this "\(quantity)"

我们可以转换成这样的字符串"\(quantity)"

回答by della pramukti raharjo

let session_id  : Int32 = (jsonObject.value(forKey: "id") as! NSNumber).int32Value

回答by Alberto Manuzzi

Swift 5:

斯威夫特 5:

I manage to fix it buy using .descriptionwhen interpolating the UserDefaults value into a label converting from Int to String.

.description当将 UserDefaults 值插入一个从 Int 转换为 String 的标签时,我设法修复了它。

working code highScoreLabel?.text = "HiScore: \(hiScoreValue.description)"

工作代码 highScoreLabel?.text = "HiScore: \(hiScoreValue.description)"

old code highScoreLabel?.text = "HiScore: \(String(describing: hiScoreValue))"

旧代码 highScoreLabel?.text = "HiScore: \(String(describing: hiScoreValue))"