iOS Swift: Could not cast value type '__NSCFNumber' to 'NSString'
The error is saying that your quantity is Number
and you cannot directly convert number to String
, try like this.
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 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.
Swift - 4 - Could not cast value of type '__NSCFNumber' (0x10cfb1840) to 'NSString' (0x10c0824a8)
The error is pretty clear: The value for key id
is a number (Int
or maybe Double
)
You can read a could-not-cast error always as
Could not cast value of the right type to my proposed wrong type
fkprofession = pickOption[pickerView.selectedRow(inComponent: 0)]["id"] as! Int
Or if you need a string
fkprofession = String(pickOption[pickerView.selectedRow(inComponent: 0)]["id"] as! Int)
Swift 4: Could not cast value of type '__NSCFNumber' to 'NSString'
In Swift 4, the String initializer requires the describing:
argument label.
I don't know if this will solve your problem, but your first line of code should be written:
self.adc_role_id = String(describing: res["adc_role_id"])
Could not cast value of type '__NSCFNumber' (0x25418e000) to 'NSString'
Error #1
Could not cast value of type '__NSCFNumber' (0x25418e000) to 'NSString'
Solution:
Cast latitude and longitude to Double
Error #2
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
Solution:
Assign the dictionary values to variables and then concatenate the string with Sting Interpolation
let city = wordsResultSet["cityName"] as! String
let name = wordsResultSet["name"] as! String
let latitude = wordsResultSet["latitude"] as! Double
let longitude = wordsResultSet["longitude"] as! Double
let aWord = "\(city) , \(name)-$-\(latitude)-$-\(longitude)"
Could not cast value of type '__NSCFNumber' (0x1105dc3c0) to 'NSString' Swift 3
It's a floating point not a string. cast is to float
let aud = rateDict["AUD"] as! Float
Edit: Avoid force casting, as that may crash your app if the data is not expected.
Using code below will prevent crashes
if let rateDict = dict["rates"] as? [String : Any] {
if let aud = rateDict["AUD"] as? Float {
print("\(aud)")
}
}
Could not cast value of type '__NSCFNumber' (0x111189058) to 'NSString' error
Replace this
self.stringWallet = result["wallet_amount"] as! String
with
if let value = result["wallet_amount"] as? NSNumber {
self.stringWallet = value.stringValue
}
and any casting line with same value , as the type of wallet_amount
value is NSNumber
not String
Could not cast value of type '_SwiftValue' error
The error reason is that Swift 3 AnyObject?
cannot be cast directly to Foundation NSString
.
You could write
let ft_of_time = ((anyObj["ft_of_time"] as! String as NSString).substring(to: 5) as NSString)
But I recommend to replace the entire line with
let ftOfTime = anyObj["ft_of_time"] as! String
let ft_of_time = ftOfTime.substring(to:ftOfTime.index(ftOfTime.startIndex, offsetBy: 5)) as NSString
It does the same but it uses native Swift API. Except the final cast to NSString
(if needed at all) there is no type casting back and forth.
Related Topics
Could Not Cast Value of Type 'Uitableviewcell' to '(Appname).(Customcellname)'
iOS Check If Application Has Access to Microphone
React Native Xcode Project Product Archive Fails with Duplicate Symbols for Architecture Arm64
Swift 3 - How to Make Timer Work in Background
How Detect Swipe Gesture Direction
Get Country Code from Country Name in iOS
Unnotificationattachment with Uiimage or Remote Url
Having App Restart Itself When It Detects Change to Privacy Settings
How to Get Notified in Contact Changed Event in iOS
How to Get the Front Camera in Swift
Path Extension and Mime Type of File in Swift
Can't Load Uiviewcontroller Xib File in Storyboard in Swift
Does Webkit in iOS 11 (Beta) Support Webrtc
Constant Movement in Spritekit
How to Find the Address of a Stack Trace in Lldb for iOS